python爬虫爬取图片代码
时间: 2023-09-23 09:10:10 浏览: 132
以下是一个简单的Python爬虫爬取图片的代码:
```python
import requests
from bs4 import BeautifulSoup
import os
# 获取页面内容
def get_html(url):
response = requests.get(url)
html = response.content
return html
# 解析页面内容,获取图片链接
def get_img_links(html):
soup = BeautifulSoup(html, 'html.parser')
img_links = []
for img in soup.find_all('img'):
img_link = img.get('src')
if img_link not in img_links:
img_links.append(img_link)
return img_links
# 下载图片
def download_img(img_link, folder_name):
response = requests.get(img_link)
img_name = img_link.split('/')[-1]
folder_path = f'./{folder_name}'
if not os.path.exists(folder_path):
os.makedirs(folder_path)
img_path = f'{folder_path}/{img_name}'
with open(img_path, 'wb') as f:
f.write(response.content)
print(f'{img_name} 下载完成!')
if __name__ == '__main__':
url = 'https://www.example.com'
html = get_html(url)
img_links = get_img_links(html)
for img_link in img_links:
download_img(img_link, 'images')
```
在代码中,我们使用了`requests`库发送HTTP请求获取页面内容,使用`BeautifulSoup`库解析页面内容,获取图片链接。然后我们可以通过`requests`库下载图片并保存到本地。最后,我们可以将所有的图片保存到一个名为`images`的文件夹中。
阅读全文