写一段python 可以爬取懂车帝汽车图片的程序
时间: 2024-06-10 14:05:45 浏览: 143
好的,以下是可以爬取懂车帝汽车图片的Python程序:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.dongchedi.com/pictures/'
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'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
for picture in soup.find_all('img', class_='lazy'):
image = picture.get('data-original')
if image:
response = requests.get(image, headers=headers)
with open(image.split('/')[-1], 'wb') as f:
f.write(response.content)
print('Successfully downloaded', image.split('/')[-1])
```
这段程序通过requests获取网页的HTML内容,并使用BeautifulSoup来解析HTML。然后,我们通过循环查找所有class为“lazy”的img标签,获取图片链接并使用requests下载图片,将图片保存在本地。
阅读全文