虎牙爬虫--无法保存
求求求,我用pycharm做的爬虫,我建立了picture文件夹,但是为什么我保存不了。我找吐了。报错内容:
File "C:\Users\Administrator\AppData\Roaming\JetBrains\PyCharmCE2021.3\scratches\scratch.py", line 30, in <module>
with open('picture/' + str(n) + '.jpg', 'wb') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'picture/直播.jpg'
代码:
import requests
import re
#1.模拟浏览器发起请求
url = 'https://www.'
header={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
#发起请求
response=requests.get(url,headers=header)
#3.对内容解析
img=re.findall(' <img class="pic" data-original="(.*?)"',response.text)
name =re.findall('data-default-img="338x190" alt="(.*?)"',response.text)
print(zip(img,name))
print(list(zip(img,name)))
#4.保存内容
for i,n in zip(img,name):
result=requests.get(i)
with open('picture/' + str(n) + '.jpg', 'wb') as file:
file.write(result.content)
print(n+'打印完成')