几行python代码获取豆瓣图片批量下载

2018年11月14日21:20:48 发表评论 1,330 views
import requests
from bs4 import BeautifulSoup
import urllib.request
import lxml
x = 1
#获取豆瓣网妹子图片
def getGirlsImage(page = 1):
    url = "https://www.dbmeinv.com/dbgroup/show.htm?cid=4&pager_offset={}".format(page)
    #此函数在url中传入page,用来翻页
    response = requests.get(url)
    html = response.text
    #创建对象,解析网页lxml
    soup = BeautifulSoup(html,"html.parser")
    girl = soup.find_all("img")

    for i in girl:
        global x #此处要用全局变量
        imgsrc = i.get(("src"))

        print("正在下载{}".format(x))
        urllib.request.urlretrieve(imgsrc,'C:/image/%s.jpg'%x)
        x+=1

#下载图片
def download():
    for i in range(1,11):
        print('正在下载第{}页图片'.format(i))

        getGirlsImage(i)
download()

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: