地级市的城市接龙,兼容同音字。从百度百科上爬取了中国的城市,共338个地级市。
import random
import requests
import re
from bs4 import BeautifulSoup
from pypinyin import pinyin
# 抓取城市数据
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0',
'Connection':'keep-alive'}
s = requests.Session()
s.headers.update(headers)
# 百度百科里面的城市
absolute_url = "https://baike.baidu.com/item/%E4%B8%AD%E5%9B%BD%E5%9F%8E%E5%B8%82%E6%96%B0%E5%88%86%E7%BA%A7%E5%90%8D%E5%8D%95/12702007?fr=aladdin"
html = s.get(absolute_url).content
soup = BeautifulSoup(html,"lxml")
citySoup = soup(text=re.compile("市、"))
citys = []
for i in citySoup:
for city in i.split('、'):
citys.append(city.rstrip("自治州").rstrip("市"))
# 运行城市接龙
while True:
city_input = input("请输入一个城市:\n")
if city_input == 'q':
break
if city_input in citys:
mcity = []
for c in citys:
if city_input.endswith(c[0]):
mcity.append(c)
# 兼容同音字
elif pinyin(city_input)[-1] == pinyin(c)[0]:
mcity.append(c)
if mcity:
city = random.choice(mcity)
print(city)
else:
print("没有匹配到城市")
else:
print("您输入的不是中国的地级市")