给我一个爬取bing关于某关键字图片的Python爬虫代码
时间: 2023-05-15 16:05:27 浏览: 119
以下是一个简单的Python爬虫代码,可以爬取Bing搜索引擎上与关键字相关的图片:
```python
import requests
from bs4 import BeautifulSoup
keyword = input("请输入关键字:")
url = "https://www.bing.com/images/search?q=" + keyword
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
img_tags = soup.find_all('img')
urls = []
for img in img_tags:
urls.append(img.get('src'))
for i, url in enumerate(urls):
response = requests.get(url)
with open(keyword + str(i) + '.jpg', 'wb') as f:
f.write(response.content)
```
你可以将关键字输入到程序中,然后运行它,它将会在当前目录下创建一系列以关键字命名的图片文件,这些图片是从Bing搜索引擎上获取的。