5、从指定网站(https://travel.qunar.com/p-cs300195-hangzhou-meishi)中爬取需要的杭州美食信息,包括页面的餐厅名称、星级、地址等信息,结果要求保存在与代码文件相同地址的目录下,文件名称为“qunar.csv”
时间: 2024-04-23 08:27:01 浏览: 163
以下是使用Python的代码示例:
```python
import requests
from bs4 import BeautifulSoup
import csv
url = 'https://travel.qunar.com/p-cs300195-hangzhou-meishi'
res = requests.get(url)
soup = BeautifulSoup(res.text, 'html.parser')
restaurants = soup.select('.list_item')
with open('qunar.csv', 'w', encoding='utf-8', newline='') as f:
writer = csv.writer(f)
writer.writerow(['餐厅名称', '星级', '地址'])
for restaurant in restaurants:
name = restaurant.select_one('.ellipsis').text
star = len(restaurant.select('.icon_star'))
address = restaurant.select_one('.address').text
writer.writerow([name, star, address])
```
代码中使用requests库发送HTTP请求,并使用BeautifulSoup库进行HTML解析,然后使用csv库将数据保存到CSV文件中。
阅读全文