import requests
from bs4 import BeautifulSoup
url="https://www.tianqi.com/chinacity.html"
header={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"}
res=requests.get(url=url,headers=header)
html = res.text.encode('iso-8859-1')
soup = BeautifulSoup(html,"html.parser",from_encoding="utf-8")#获取所以的a
city = soup.find('div',class_="citybox")
a=city.find_all('a')
f=open('city.txt','w',encoding='utf-8')for di in a:
contet = di.get_text()print(contet)
f.write(contet+'\n')# h2=city.find_all('h2')# span=city.find_all('span')# for link in h2:# print(link.get_text())# for s in span:# print(s.get_text())# f=open('city.txt','w',encoding='utf-8')# for li in div:# contents=li.xpath('.//a//text()')[0]# print(contents+'\n')# f.write(contents)