import requests
import re
def get_images_list(page):
"""批量获取斗图网图片"""
# 获取每一页所有图的网址
response = requests.get("http://www.doutula.com/photo/list/?page={}".format(page))
html = response.text
# 正则匹配每一幅图的网址
reg ='data-original="(.*?)".*?alt="(.*?)"'
# 增加效率
# .S 多行匹配
reg = re.compile(reg, re.S)
image_list = re.findall(reg, html)
return image_list
def get_images(image_list):
"""下载图片"""
for image in image_list:
image_address = image[0]
image_name = image[1]
url =image_address
response = requests.get(image_address)
data = response.content
# 将表情包下载到本地存入文件夹中
with open("C:\\Users\\wwxy\\Desktop\\project\\"+image_name+".jpg", 'wb') as f:
# 写入本地
f.write(data)
if __name__ == '__main__':
for i in range(1,2):
image_list = get_images_list(i)
get_images(image_list)
print("已下载第"+str(i)+"页表情包")
思路:翻页网页源代码中有每一张图片的网址