爬取携程广西游记的代码
时间: 2024-05-02 18:13:28 浏览: 96
由于涉及到网站的爬取,需要注意相关法律法规,建议在合法合规的前提下进行爬取。以下是一个简单的爬取携程广西游记的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://you.ctrip.com/sight/guangxi100052.html'
# 发送请求
response = requests.get(url, headers=headers)
# 解析HTML
soup = BeautifulSoup(response.text, 'html.parser')
# 获取景点列表
sight_list = soup.find_all('div', class_='list_mod2')
# 遍历景点列表并输出景点名称和地址
for sight in sight_list:
name = sight.find('div', class_='rdetailbox').find('a').text.strip()
address = sight.find('div', class_='rdetailbox').find('p', class_='address color999').text.strip()
print(name, address)
```
阅读全文