Python3煎蛋图

煎蛋上面还是有挺多有意思的,很早之前写的,其实有很多地方需要完善。 以下是简单粗暴的下载图片,国内网站就是快,新浪还是有那么一点点良心的。

煎蛋妹子图过审标准

1. 图片请上传到新浪微博相册,在评论框里粘帖图片地址即可发图原创图请注明来源。
2. 发布后请等待审核,未通过审核的原因可能是重复图或者敏感图
我只是简单的做个记录,就不写分析了,直接上。
import re
import requests
from bs4 import BeautifulSoup

#    http://jandan.net/ooxx/page-1#comments
headers={'referer':'http://jandan.net/', 'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0'}
url = "http://jandan.net/ooxx/page-"

count = 1
#  下载图片
def download_img(url):
    global count
    image = "http:" + url
    print(image)
    with open('D:/imgs/%s.jpg' % count, 'wb') as file:
        try:
            image_get = requests.get(image, headers=headers, allow_redirects=False)
            if image_get.status_code == 200:
                img_data = image_get.content
                file.write(img_data)
                count += 1
            else:
                count += 1
                return
        except:
            print("下载图片出错")
            return

#  返回出 单个图片地址
def download_url(imags_date):
    for imag in imags_date:
        imag_url = imag["href"].strip().replace('http:', '')
        download_img(imag_url)

# 遍历所有的图片页 返回 图片地址集合
for page in range(1, 2367):#这里是页数,自己可以随便选
    new_url = url + str(page) + "#comments"
    try:
        html = requests.get(new_url, headers=headers)
        if html.status_code == 200:
            print(new_url, page)
            soup = BeautifulSoup(html.text, "lxml")
            soup_img = soup.find_all("a", {"href": re.compile("//.*?\.jpg")})
            download_url(soup_img)
        else:
            print("这一页出现了一个错误", page)
            continue
    except:
        print("错误")
        continue

其实可以优化很多地方

  • 开多线程进程提高下载速度
  • 更改正则表达式可以下载.gif图片
  • 可以通过点赞数来筛选图片