|

楼主 |
发表于 2023-3-16 21:15:35
|
显示全部楼层
已解决,在headers加上防盗链就行了
源码:
import requests
from bs4 import BeautifulSoup
import time
domain = "https://www.keaidian.com"
url = "https://www.keaidian.com/gexingtouxiang/6426.html"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
#设置防盗链
"Referer": "https://www.keaidian.com/gexingtouxiang/6426.html"
}
resp = requests.get(url, headers=headers)
resp.encoding = 'UTF-8'
n = 1
main_page = BeautifulSoup(resp.text, "html.parser")
a_list = main_page.findAll("a", attrs={"class": "swipebox"})
for a in a_list:
href = a.get("href")
child_href = domain + a.get("href")
img_scr = requests.get(child_href, headers=headers)
with open(f"D:/pythonxue/text/tupian/{n}.jpg", "wb") as f:
f.write(img_scr.content)
print(f"图片{n}下载完毕")
n += 1
time.sleep(10) # 增加延迟,可以适当调整这个值 |
|