Python爬取全国旅游景点代码
时间: 2023-10-07 15:08:37 浏览: 74
你可以使用Python中的requests和BeautifulSoup库来爬取全国旅游景点的数据。以下是一个简单的爬取示例:
```python
import requests
from bs4 import BeautifulSoup
url = "http://you.ctrip.com/sightseeing/"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
# 定位到景点列表所在的标签
spot_list = soup.find("div", {"class": "list_wide_mod2"})
# 遍历景点列表,提取景点名称和代码
for spot in spot_list.find_all("li"):
name = spot.find("div", {"class": "list_mod2_tit"}).text.strip()
code = spot.find("a")["href"].split("/")[-2]
print(name, code)
```
注意:在爬取网站数据时,请务必遵守网站的规定和法律法规。此外,该网站有反爬虫机制,需要设置 User-Agent 和 Cookie 才能成功爬取。
相关问题
Python爬取苏州市旅游景点代码
你可以使用Python中的requests和BeautifulSoup库来爬取苏州市旅游景点的数据。以下是一个简单的爬取示例:
```python
import requests
from bs4 import BeautifulSoup
url = "http://www.suzhou.gov.cn/zwgk/zwdt/jrsz/index.html" # 苏州市旅游景点网址
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
# 定位到景点列表所在的标签
spot_list = soup.find("div", {"class": "list_con"})
# 遍历景点列表,提取景点名称和代码
for spot in spot_list.find_all("li"):
name = spot.find("a").text
code = spot.find("span").text
print(name, code)
```
注意:在爬取网站数据时,请务必遵守网站的规定和法律法规。
python爬取途牛旅游网景点信息
好的,您可以使用 Python 爬取途牛旅游网景点信息。以下是一个简单的爬虫示例代码,可以帮助您进行参考:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://s.tuniu.com/search_complex/whole-sichuan-0-%E6%97%85%E6%B8%B8%E6%99%AF%E7%82%B9/1/'
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'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
items = soup.select('.search-list .item')
for item in items:
title = item.select_one('.title a').text.strip()
price = item.select_one('.price em').text.strip()
print(title, price)
```
这段代码会爬取途牛旅游网四川省的旅游景点信息。您需要根据您需要爬取的信息的具体位置和标签来修改代码。同时,注意爬虫的合法性,不要频繁请求网站,避免对网站造成负担。