帮我写个飞猪酒店的爬虫程序
时间: 2023-12-20 22:28:32 浏览: 25
根据提供的引用内容,可以使用Python的requests库对飞猪酒店网站进行爬取。以下是一个简单的爬虫程序的框架:
1. 导入requests库和BeautifulSoup库
2. 设置请求头,模拟浏览器访问
3. 构造请求URL,包括城市、关键词、入住时间和离店时间等参数
4. 发送请求,获取响应内容
5. 解析响应内容,提取需要的数据
6. 保存数据到本地文件或数据库中
具体实现细节可以参考以下代码:
```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
city = '上海'
keywords = '浦东'
check_in = '2021-08-01'
check_out = '2021-08-02'
url = f'https://hotel.fliggy.com/hotel_list3.htm?_input_charset=utf-8&_output_charset=utf-8&cityName={city}&city=&keywords={keywords}&checkIn={check_in}&checkOut={check_out}'
# 发送请求,获取响应内容
response = requests.get(url, headers=headers)
html = response.text
# 解析响应内容,提取需要的数据
soup = BeautifulSoup(html, 'html.parser')
hotel_list = soup.find_all('div', class_='item')
for hotel in hotel_list:
name = hotel.find('h4', class_='title').text.strip()
address = hotel.find('p', class_='address').text.strip()
price = hotel.find('span', class_='price').text.strip()
print(name, address, price)
# 保存数据到本地文件或数据库中
# ...
```