将这段代码改一下只将爬取的前十张图片存入到python picture文件夹中import requests head={ "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36" } re=requests.get(url="https://www.iqiyi.com/?vfm=f_208_dbq&fv=88cd0334c6d6e1bf") print(type(re)) html=re.text print(html) from bs4 import BeautifulSoup soup=BeautifulSoup(html,'lxml') listtest1=soup.find_all("picture",attrs={"class":"video-item-preview-img"}) print(listtest1) print(len(listtest1)) print(listtest1[0]["id"]) for p in listtest1: print(p["id"]) re=requests.get("http:"+p["id"]) with open(p["id"].split('/')[-1]+'.jpg','wb') as f: f.write(re.content)
时间: 2024-02-29 17:56:54 浏览: 33
以下是修改后的代码,只将爬取的前十张图片存入到Python picture文件夹中:
```python
import requests
import os
from bs4 import BeautifulSoup
head = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
}
re = requests.get(url="https://www.iqiyi.com/?vfm=f_208_dbq&fv=88cd0334c6d6e1bf")
html = re.text
soup = BeautifulSoup(html, 'lxml')
listtest1 = soup.find_all("picture", attrs={"class": "video-item-preview-img"})
if not os.path.exists("picture"):
os.makedirs("picture")
count = 0
for p in listtest1:
if count >= 10:
break
print(p["id"])
re = requests.get("http:" + p["id"])
with open(os.path.join("picture", p["id"].split('/')[-1] + '.jpg'), 'wb') as f:
f.write(re.content)
count += 1
```
在上述代码中,首先添加了一个判断语句,判断是否存在名为 "picture" 的文件夹,如果没有则创建一个。然后通过一个计数器 count 来控制只下载前十张图片。同时在保存图片时,使用了 `os.path.join()` 函数,将文件路径拼接成一个完整的路径,避免了操作系统差异导致的路径错误问题。