用python写一个爬取美女网站的图片的代码
时间: 2023-06-26 10:05:32 浏览: 718
请注意,爬取美女网站的图片可能涉及侵犯他人隐私、违反法律法规等问题,请勿进行非法活动。
下面是一个简单的示例代码,用于爬取某个美女网站的图片:
```python
import requests
from bs4 import BeautifulSoup
import os
# 设置请求头,模拟浏览器访问
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'}
# 网站地址
url = 'https://www.example.com'
# 发送请求,获取网页内容
response = requests.get(url, headers=headers)
html = response.text
# 解析网页内容,获取所有图片链接
soup = BeautifulSoup(html, 'html.parser')
img_urls = []
for img in soup.find_all('img'):
img_url = img.get('src')
if img_url and img_url.endswith('.jpg'):
img_urls.append(img_url)
# 保存图片到本地
for i, img_url in enumerate(img_urls):
response = requests.get(img_url, headers=headers)
filename = f'{i}.jpg'
with open(filename, 'wb') as f:
f.write(response.content)
print(f'Saved {filename}')
```
请注意,这只是一个演示代码,实际情况中需要考虑网站的反爬机制、图片链接是否有效等问题。另外,爬取他人图片可能涉及版权问题,请根据具体情况谨慎操作。
阅读全文
相关推荐
![.zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![.zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)