世界,您好!
内容纲要
欢迎使用WordPress。这是您的第一篇文章。编辑或删除它,然后开始写作吧!
标题
内容
python代码
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
from fake_useragent import UserAgent
from lxml import etree
import time
import pandas as pd
import os
if not os.path.exists('./weather'):
os.mkdir('./weather')
class weather(object):
def spider(self, url):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
response = requests.get(url=url, headers=headers).content.decode()
tree = etree.HTML(response)
return tree
def homepage(self):
home_page_url = 'https://lishi.tianqi.com/'
tree = self.spider(home_page_url)
city_list = tree.xpath("//tbody/tr/td/ul[@class='table_list']/li/a/text()")
href_list = tree.xpath("//tbody/tr/td/ul[@class='table_list']/li/a/@href")
print(city_list)
print(href_list)
#判断地点
while True:
city_name = input('请输入城市的名字\n')
if city_name in city_list:
city_index = city_list.index(city_name)
city_url = href_list[city_index]
break
else:
print('输入错误')
return city_url, city_name
def weather_cheak(self, city_url):
city_url = 'https://lishi.tianqi.com/'+ city_url
#判断日期,若不判断,超出后自动转跳至index.html
while True:
now_month = time.strftime('%Y%m', time.localtime())
weather_time = input('请输入查询的时间(格式202104)\n')
if weather_time <= now_month:
city_url = city_url.replace('index', weather_time)
print(city_url)
break
else:
print('日期错误,请重新输入')
tree = self.spider(city_url)
date = tree.xpath("//div[@class='tian_three']/ul[@class='thrui']/li/div[1]/text()")
high_temperature = tree.xpath("//div[@class='tian_three']/ul[@class='thrui']/li/div[2]/text()")
low_temperature = tree.xpath("//div[@class='tian_three']/ul[@class='thrui']/li/div[3]/text()")
detail_weather = tree.xpath("//div[@class='tian_three']/ul[@class='thrui']/li/div[4]/text()")
wind_direction = tree.xpath("//div[@class='tian_three']/ul[@class='thrui']/li/div[5]/text()")
print(date)
print(high_temperature)
print(low_temperature)
print(detail_weather)
print(wind_direction)
return date, high_temperature, low_temperature, weather_time, detail_weather, wind_direction
def Tocsv(self, date ,high_temperature ,low_temperature, city_name, weather_time, detail_weather, wind_direction):
DF = pd.DataFrame(
{
'日期':date,
'最高温度':high_temperature,
'最低温度':low_temperature,
'详细天气':detail_weather,
'风向':wind_direction
}
)
print(DF)
path = f'./weather/weather_{city_name}_{weather_time}.csv'
DF.to_csv(path, index=0)
def run(self):
city_url, city_name = self.homepage()
date, high_temperature, low_temperature, weather_time, detail_weather, wind_direction= self.weather_cheak(city_url)
self.Tocsv(date, high_temperature, low_temperature, city_name, weather_time, detail_weather, wind_direction)
if __name__ == "__main__":
city = weather()
city.run()
print('over!')
版权声明:
作者:bandao
链接:https://www.bandao.ltd/daily_life/hello-world/
来源:个人学习分享
文章版权归作者所有,未经允许请勿转载。
THE END
二维码
文章目录
关闭
共有 0 条评论