Python爬取苏州市旅游景点代码
时间: 2023-10-07 19:08:37 浏览: 45
你可以使用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中的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和beautifulsoup4库,然后按照注释修改代码中的参数,即可运行。
```python
import requests
from bs4 import BeautifulSoup
# 修改下面的参数为目标景点的网址
url = 'https://www.tripadvisor.com/Attraction_Review-g187791-d193607-Reviews-Colosseum-Rome_Lazio.html'
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'
}
# 发送GET请求
r = requests.get(url, headers=headers)
# 解析HTML文档
soup = BeautifulSoup(r.text, 'html.parser')
# 提取景点名称
name = soup.find('h1', {'class': 'ui_header h1'}).text.strip()
# 提取景点地址
address = soup.find('span', {'class': '_2saB_OSe'}).text.strip()
# 提取景点评分
rating = soup.find('span', {'class': 'r2Cf69qf'}).text.strip()
# 输出结果
print('名称:', name)
print('地址:', address)
print('评分:', rating)
```
希望这个代码片段可以帮助你。