python爬取去哪儿网
时间: 2023-10-02 21:05:07 浏览: 60
你可以使用 Python 中的 requests 和 BeautifulSoup 库来爬取去哪儿网上的数据。下面是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = "https://hotel.qunar.com/city/xian/"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# 查找所有酒店名称和价格
hotels = soup.find_all("div", class_="item_hotel_info")
for hotel in hotels:
name = hotel.find("a", class_="hotel-name-link").text.strip()
price = hotel.find("span", class_="item_price").text.strip()
print(name, price)
```
这段代码可以爬取去哪儿网上的西安酒店列表,并输出每个酒店的名称和价格。你可以根据自己的需求修改代码以爬取其他页面或获取其他信息。需要注意的是,爬取网站数据时一定要遵守网站的爬虫协议,不要过于频繁地请求同一个页面,以免被网站封禁 IP。
相关问题
python爬取去哪儿网景点
要爬取去哪儿网的景点信息,你可以使用Python中的requests来模拟浏览器发起请求,然后用BeautifulSoup或者xpath等库来解析网页内容,获取需要的数据。具体步骤如下:
1. 导入requests和BeautifulSoup库
```
import requests
from bs4 import BeautifulSoup
```
2. 设置请求头,模拟浏览器进行请求
```
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 = "https://travel.qunar.com/p-cs299878-shanghai-jingdian"
response = requests.get(url, headers=headers)
```
3. 解析网页内容,获取需要的数据
```
soup = BeautifulSoup(response.text, 'html.parser')
items = soup.find_all('div', class_='list_item')
for item in items:
name = item.find('span', class_='cn_tit').text.strip()
location = item.find('span', class_='area').text.strip()
score = item.find('span', class_='total_star').text.strip()
comment_num = item.find('a', class_='comment_sum').text.strip()
print(name, location, score, comment_num)
```
以上代码可以爬取去哪儿网上上海的景点信息,包括景点名称、所在地区、评分和评论数。
python爬取去哪儿网导入数据库
好的,关于Python爬取去哪儿网并导入数据库的问题,我可以给你一些参考意见。首先,你需要编写Python爬虫程序,在去哪儿网上爬取所需的数据。接着,你可以使用Python自带的SQLite模块或者第三方模块如MySQLdb来连接数据库,并将爬取的数据存储到指定数据库中。当然,具体的实现过程还需要根据你的需求和具体情况来进行具体的代码编写和调试。如果你需要更详细的指导,可以参考相关的Python爬虫教程和数据库操作教程。