python爬取携程景点数据
时间: 2024-04-25 19:17:34 浏览: 189
基于python实现爬取携程景点数据与评论数据源码+项目说明.zip
5星 · 资源好评率100%
Python可以使用多种库和框架来进行网页爬取,其中比较常用的是BeautifulSoup和Scrapy。下面是使用BeautifulSoup进行携程景点数据爬取的简单示例:
1. 首先,你需要安装BeautifulSoup库。可以使用以下命令进行安装:
```
pip install beautifulsoup4
```
2. 导入所需的库:
```python
from bs4 import BeautifulSoup
import requests
```
3. 发送HTTP请求并获取网页内容:
```python
url = "https://you.ctrip.com/sight/beijing1.html"
response = requests.get(url)
```
4. 使用BeautifulSoup解析网页内容:
```python
soup = BeautifulSoup(response.text, "html.parser")
```
5. 定位到包含景点信息的HTML元素,并提取相关数据:
```python
sight_list = soup.find_all("div", class_="list_wide_mod2")
for sight in sight_list:
name = sight.find("h3").text
address = sight.find("p", class_="address color999").text
score = sight.find("span", class_="score").text
print("景点名称:", name)
print("地址:", address)
print("评分:", score)
print("--")
```
以上代码会爬取携程网站上北京市的景点数据,并打印出景点名称、地址和评分。
阅读全文