用python30写一个爬取图片的代码
时间: 2024-02-21 09:58:54 浏览: 62
好的,以下是一个使用Python3爬取图片的示例代码,你可以根据自己的需求进行修改:
```python
import requests
from bs4 import BeautifulSoup
# 爬取网页
url = "https://unsplash.com/search/photos/dog"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 解析网页获取图片链接
images = soup.find_all('img', class_='_2zEKz')
for img in images:
img_url = img['src']
print(img_url)
# 下载图片
response = requests.get(img_url)
with open('dog.jpg', 'wb') as f:
f.write(response.content)
```
这个示例代码使用了Requests库来获取网页内容,使用了BeautifulSoup库来解析HTML代码,然后在HTML代码中找到图片链接,并使用Requests库下载图片。你可以将示例中的网址和图片名替换成你所需的内容,以便下载不同的图片。请注意:在Python3中,`print`函数需要使用括号,即`print(img_url)`。
相关问题
用python写一个爬取图片的代码
好的,以下是一个使用Python爬取图片的示例代码,你可以根据自己的需求进行修改:
```python
import requests
from bs4 import BeautifulSoup
# 爬取网页
url = "https://unsplash.com/search/photos/dog"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 解析网页获取图片链接
images = soup.find_all('img', class_='_2zEKz')
for img in images:
img_url = img['src']
print(img_url)
# 下载图片
response = requests.get(img_url)
with open('dog.jpg', 'wb') as f:
f.write(response.content)
```
这个示例代码使用了Requests库来获取网页内容,使用了BeautifulSoup库来解析HTML代码,然后在HTML代码中找到图片链接,并使用Requests库下载图片。你可以将示例中的网址和图片名替换成你所需的内容,以便下载不同的图片。
用Python写一个爬取图片的代码
下面是一个简单的使用Python爬取图片的示例代码:
```python
import requests
from bs4 import BeautifulSoup
import urllib
# 目标网站的URL地址
url = "https://www.example.com"
# 发送GET请求获取网页内容
response = requests.get(url)
# 使用BeautifulSoup解析网页内容,提取图片URL列表
soup = BeautifulSoup(response.content, features="html.parser")
img_tags = soup.find_all("img")
img_urls = [img["src"] for img in img_tags]
# 循环遍历图片URL列表,下载图片到本地
for url in img_urls:
filename = url.split("/")[-1]
urllib.request.urlretrieve(url, filename)
print("下载图片:%s" % filename)
```
上面的代码中,首先使用requests库发送GET请求获取网页内容,然后使用BeautifulSoup库解析网页内容,提取所有的图片URL列表。最后使用urllib库中的urlretrieve()方法循环遍历图片URL列表,下载图片到本地,并输出下载图片的文件名。需要注意的是,需要根据实际情况对网页内容以及图片URL的解析进行调整。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](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)
![](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)
![](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)
![](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)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)