python爬取汽车之家的口碑
时间: 2023-11-21 15:56:26 浏览: 160
以下是使用Python爬取汽车之家口碑的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 设置请求头
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'}
# 构造请求URL
url = 'https://koubei.autohome.com.cn/view/202110/1/0/0.html'
# 发送请求
response = requests.get(url, headers=headers)
# 解析HTML
soup = BeautifulSoup(response.text, 'html.parser')
# 获取购车目的和车系名称
result = {}
purpose = ','.join([i['purposename'] for i in soup.select('.choose-con')[0].select('.choose-dl')[0].select('.choose-dd')[0].select('a')])
boughtcityname = soup.select('.choose-con')[0].select('.choose-dl')[1].select('.choose-dd')[0].select('a')[0].text
result['purpose'] = purpose
result['boughtcityname'] = boughtcityname
# 输出结果
print(result)
```
阅读全文