用Python语言编写爬取百度图片中猫和狗的图片各100张,将图片存入相应文件夹
时间: 2024-06-10 12:04:44 浏览: 201
好的,以下是Python代码:
```python
import os
import requests
from bs4 import BeautifulSoup
def download_images(url, folder, num):
if not os.path.exists(folder):
os.mkdir(folder)
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
items = soup.find_all('img')
count = 0
for item in items:
if 'src' in item.attrs:
if 'jpg' in item['src'] or 'png' in item['src']:
img_url = item['src']
response = requests.get(img_url)
if response.status_code == 200:
file_path = os.path.join(folder, f'{count}.jpg')
with open(file_path, 'wb') as f:
f.write(response.content)
count += 1
if count == num:
break
if __name__ == '__main__':
cat_url = 'https://www.baidu.com/s?tn=80035161_1_dg&ie=utf-8&word=%E7%8C%AB'
dog_url = 'https://www.baidu.com/s?tn=80035161_1_dg&ie=utf-8&word=%E7%8B%97'
cat_folder = 'cat'
dog_folder = 'dog'
num = 100
download_images(cat_url, cat_folder, num)
download_images(dog_url, dog_folder, num)
```
在脚本中,我们首先定义`download_images`函数,该函数使用`requests`和`BeautifulSoup`库来解析网页和获取图片。然后,我们定义了两个URL:一个是用于猫的图片搜索结果的URL,另一个是用于狗的图片搜索结果的URL。我们还定义了两个文件夹,以存储下载的图像。我们使用`download_images`函数从每个URL下载100张图像,并将它们存储在相应的文件夹中。
阅读全文