Python怎么保存爬虫图片(爬虫.保存.图片.Python...)
python 中保存爬取的图片有三种方法:使用 urllib.request 模块的 urlretrieve() 函数直接下载图片。使用 requests 库的 get() 方法下载图片并使用 open() 函数写入文件。使用第三方库(如 beautiful soup 或 scrapy)获取图片 url 后,再使用上述方法保存图片。
如何在 Python 中保存爬取的图片
方法 1:使用 urllib.request
- 导入 urllib.request 模块。
- 打开要下载的图片的 URL。
- 使用 urlretrieve() 函数将图片保存到本地文件中。
PHP
import urllib.request
url = 'https://example.com/image.jpg'
urllib.request.urlretrieve(url, 'image.jpg')
方法 2:使用 requests
- 导入 requests 模块。
- 获取图片的 URL。
- 使用 get() 函数发送请求。
- 获取响应内容。
- 使用 open() 函数以二进制模式写入文件。
PHP
import requests
url = 'https://example.com/image.jpg'
response = requests.get(url)
with open('image.jpg', 'wb') as f:
f.write(response.content)
方法 3:使用第三方库
- 安装 Beautiful Soup 或 Scrapy 等第三方库。
- 使用库中的方法获取图片的 src 属性。
- 使用上述方法之一保存图片。
示例(使用 Beautiful Soup):
PHP
from bs4 import BeautifulSoup
html_content = '''@@##@@'''
soup = BeautifulSoup(html_content, 'html.parser')
image_url = soup.find('img')['src']
urllib.request.urlretrieve(image_url, 'image.jpg')

以上就是Python怎么保存爬虫图片的详细内容,更多请关注知识资源分享宝库其它相关文章!