Python爬取包图网图片的代码
时间: 2024-03-17 11:34:08 浏览: 16
我可以提供Python爬取包图网图片的基本代码,但是由于包图网的图片版权归属问题,建议您在爬取过程中遵守相关法律法规,确保不会侵犯任何人的权益。
以下是一个示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://ibaotu.com/shipin/'
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.content, 'html.parser')
img_tags = soup.find_all('img', class_='lazy')
img_urls = [tag['data-original'] for tag in img_tags]
for idx, url in enumerate(img_urls):
response = requests.get(url, headers=headers)
with open(f'{idx}.jpg', 'wb') as f:
f.write(response.content)
```
该代码将请求包图网的首页,获取所有的图片链接,然后循环下载每一张图片并保存在本地。在实际爬取过程中,您可能需要加入反爬机制或者使用代理IP,以确保爬取成功。