帮我写一份可以根据关键词可以查询相关企业信息的代码
时间: 2024-01-11 22:02:02 浏览: 23
好的,我可以为您提供一个简单的示例代码,使用Python的requests和BeautifulSoup库,可以根据关键词查询百度企业信用网站的相关企业信息。请确保已经安装了这两个库。
```python
import requests
from bs4 import BeautifulSoup
def get_company_info(keyword):
url = 'https://www.baidu.com/s?wd=企业信用网站 ' + 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')
result = []
for item in soup.select('.result.c-container'):
title = item.select_one('.t a').text
link = item.select_one('.t a')['href']
desc = item.select_one('.c-abstract').text
result.append({'title': title, 'link': link, 'desc': desc})
return result
# 示例:
print(get_company_info('阿里巴巴'))
```
在上面的代码中,我们使用关键词拼接出了一个百度搜索的URL,然后发送请求获取到搜索结果页面的HTML代码,使用BeautifulSoup库解析HTML代码,提取出每个企业的标题、链接和描述,并返回结果列表。然后我们调用`get_company_info`函数并传入关键词`阿里巴巴`,最终输出查询到的结果。