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()