爬虫下载图片失败
爬虫新手求带小爬爬程序跑的正常,但是目标下载图片的文件夹为空,没有执行下载操作
import requests
from bs4 import BeautifulSoup
import time
import random
def getHTML(url):
head = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"}
try:
reqt = requests.get(url,headers=head)
reqt.encoding = 'utf-8'
return reqt.text
except:
return ""
#parse page
domain = "http://www.bizhi360.com/feizhuliu/"
head = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"}
html = getHTML(domain)
soup = BeautifulSoup(html,"html.parser")
alist = soup.find("div",class_="pic-list").ul.find_all("a")
for a in alist:
time.sleep(random.randint(2, 4))
href = 'http://www.bizhi360.com' + a.get('href')
child_r = requests.get(href)
child_r.encoding = 'utf-8'
child_html = child_r.text
pic_soup = BeautifulSoup(child_html,"html.parser")
#提取图片
image = pic_soup.find("div",class_="article").img
src = image.get('src')
#下载图片
image_r = requests.get(src, headers=head)
image_name = src.split("/")[-1]#图片名称
with open("Bizhi"+image_name,mode="wb") as f:
f.write(image_r.content ) #提取字节
print(f"{image_name},done!")
执行结果:
8494.jpg,done!
8468.jpg,done!
8382.jpg,done!
……
但是Bizhi文件夹为空啊,没有下载任何内容啊
求大神指点迷津 大神们今天都陪娃过节去了吗?{:10_277:} 本帖最后由 逃兵 于 2021-6-2 08:57 编辑
爬虫类的问题由于涉及网址,会触发审核机制
版主审核通过我们才能看到
这段代码本身没有问题,只是路径是在py文件下
你可以把这段代码放在Bizhi目录下运行,就能在这个目录下面找到爬到的图片
或者修改代码的打开目录
with open("完整路径\\\"+image_name,mode="wb") as f:
比如D盘下的Bizhi目录
with open("D:\\Bizhi\\\"+image_name,mode="wb") as f:
with open("Bizhi"+image_name,mode="wb") as f:
改成
with open("Bizhi\\"+image_name,mode="wb") as f:
页:
[1]