"""
Time: 2021/6/15 14:45
Author: [email protected]
Version: V1.0
File: get_weather.py
Describe: 获取城市天气情况:
1)确保输入存在的城市或县市
"""
import requests
import time
from lxml import html
from xpinyin import Pinyin
class Weather():
def get_weather(self, city):
url = "https://www.tianqi.com/{}/7/".format(city)
headers = {"User-Agent": "Mozilla/5.0",}
r = requests.get(url, headers=headers).content
s= html.fromstring(r)
weather = s.xpath('/html/body/div[7]/div[2]/div[2]/div/div[1]/text()')
t = time.strftime('| %H:%M:%S', time.localtime())
for w in weather:
dates = s.xpath('/html/body/div[7]/div[2]/div[2]/div/div[2]/text()')
for date in dates:
try:
if w is not None:
print('\n','-'*60,'\n',date,t,"\n "+w[5:]+'\n','-'*60)
except:
print('输入城市不存在,请重新输入')
def trans_to_pinyin(s