python爬虫爬取图片
时间: 2023-11-08 19:52:06 浏览: 27
你可以使用 Python 中的 requests 和 BeautifulSoup 库来爬取图片。以下是一个简单的示例:
```python
import requests
from bs4 import BeautifulSoup
url = "https://www.example.com/images"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
for img in soup.find_all("img"):
img_url = img.attrs.get("src")
if not img_url:
continue
img_response = requests.get(img_url)
filename = img_url.split("/")[-1]
with open(filename, "wb") as f:
f.write(img_response.content)
```
以上代码会爬取网页中所有的图片,并将其保存到当前目录下。你需要将 url 替换为你要爬取的网站的图片链接,以及使用适当的解析器(这里使用的是 html.parser)。另外,你还需要安装 requests 和 BeautifulSoup 两个库。
相关问题
python爬虫爬取图片src
在Python爬虫中,如果要爬取图片的src属性,你可以使用requests库和BeautifulSoup库进行处理。首先,你需要发送一个请求来获取网页的源码。然后,使用BeautifulSoup库来解析网页内容,并通过find_all函数来查找img标签。在img标签中,你可以使用get方法获取src属性的值,并将它代给下一个函数进行处理。以下是一个简单的代码示例:
```python
import requests
from bs4 import BeautifulSoup
def get_image_src(url):
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
img_tags = soup.find_all("img")
for img_tag in img_tags:
src = img_tag.get("src")
print(src) # 在这里可以对src进行进一步处理,如下载图片
# 调用函数并传入要爬取的网页url
get_image_src("http://example.com")
```
这个示例中,首先使用requests库发送请求获取网页的源码。然后,使用BeautifulSoup库对源码进行解析,并通过find_all函数查找所有的img标签。在循环中,使用get方法获取img标签的src属性值,并进行进一步处理,比如打印输出或下载图片。你可以根据自己的需求对这个代码进行修改和扩展。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Python爬虫爬取src图片](https://blog.csdn.net/weixin_54250368/article/details/122192440)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [Python爬虫爬取图片](https://blog.csdn.net/weixin_52136304/article/details/116357805)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
python爬虫爬取图片案例
当然,我可以为您提供一个Python爬虫爬取图片的案例。以下是一个简单的示例代码,使用Python的requests和BeautifulSoup库来实现:
```python
import requests
from bs4 import BeautifulSoup
import os
def download_images(url, save_dir):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 创建保存图片的目录
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# 获取所有图片标签
img_tags = soup.find_all('img')
for img_tag in img_tags:
img_url = img_tag['src']
if not img_url.startswith('http'):
img_url = url + img_url # 处理相对路径的情况
# 发送请求下载图片
response = requests.get(img_url)
filename = os.path.join(save_dir, img_url.split('/')[-1])
with open(filename, 'wb') as f:
f.write(response.content)
print(f"已下载图片: {filename}")
# 示例:爬取CSDN首页的图片
url = 'https://www.csdn.net/'
save_dir = 'images'
download_images(url, save_dir)
```
使用上述代码,您可以将CSDN首页的图片保存到指定目录中。请注意,这仅仅是一个简单的示例,实际应用中您可能需要处理更多的异常情况和逻辑。另外,请遵守网站的相关规定和法律法规,确保合法合规地进行网络爬取。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)