import requests
import re
import os
def get_data():
global header
url = "https://www.qiushibaike.com/imgrank/page/%d/"
number = int(input("输入需要爬取的总页数:"))
for ye in range(1, number + 1):
new_url = format(url % ye)
page_text = requests.get(url=new_url, headers=header).text
# print(page_text)
rex = '<img.*?src="(.*?)" alt=.*? width.*?'
list = re.findall(rex, page_text)
for link in list:
link = "https:" + link
data = requests.get(url=link, headers=header).content
filename = link.split("/")[-1]
pathname = "./tu/" + filename
with open(pathname, "wb") as f:
f.write(data)
print("图片下载成功", pathname)
print("图片下载地址为", link)
if __name__ == "__main__":
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/84.0.4147.105 Safari/537.36 "
}
if not os.path.exists("./tu") :
os.mkdir("./tu")
else:
os.rmdir("./tu")
get_data()
笔记思路入门参考
转载:https://blog.csdn.net/kingoflongevity/article/details/108347366
查看评论