"""
Time: 2021/6/15 14:45
Author: xiang_qu@huacenav.com
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,'
如何用python写一个天气预报小程序,安排
最新推荐文章于 2024-10-04 22:42:44 发布