好友
阅读权限10
听众
最后登录1970-1-1
|
#抓取历史天气的代码自己可以更改
#提取excel会多一行排序自己删除 python记得添加库不然不能运行
import timeimport pandas as pd
import requests
import xlwt
from bs4 import BeautifulSoup
headers = {
'User-Agent': 'Mozilla/5.0'}
url = "http://lishi.tianqi.com/haikou/202210.html"#修改url可获取不同城市的历史天气 不同城市改拼音就可以 时间202210改这个年月
weather_str = requests.get(url, headers=headers) # 获取数据
weather_str_content = weather_str.content # 赋值
# print(weather_str_content)
soup = BeautifulSoup(weather_str_content, 'html.parser') # 解析数据html可以用xlwt' 解析
soup_ul = soup.find('ul', class_='thrui') # html页面获取polt
# print(soup_ul)
# 提取数据
soup_li_list = soup_ul.find_all("li") # 获取全部的第2层
weather_time1, weather_data1, max_tem1, min_tem1, weather_win1, soup_win1 = [], [], [], [], [], [] # 统一命名
for i in soup_li_list[0:]: # 循环外框数据取得一个数值
# 天气
weather_data = i.text.split() # 将循环列表提取只提取文本信息
# print(weather_data)
weather_time1.append(''.join(weather_data[0]))
weather_data1.append(''.join(weather_data[1:2]))
max_tem1.append(''.join(weather_data[2:3]))
min_tem1.append(''.join(weather_data[3:4]))
weather_win1.append(''.join(weather_data[4:5]))
soup_win1.append(''.join(weather_data[5:7]))
weather_list1 = pd.concat = ([weather_time1, weather_data1, max_tem1, min_tem1, weather_win1, soup_win1])
workbook = xlwt.Workbook()
sheet = workbook.add_sheet("beijing")
sheet.data = pd.DataFrame()
sheet.data['年月'] = weather_time1
sheet.data['星期'] = weather_data1
sheet.data['最高气温'] = max_tem1
sheet.data['最低气温'] = min_tem1
sheet.data['天气'] = weather_win1
sheet.data['风向'] = soup_win1
# aa = DataFrame = pd.DataFrame()
print(sheet.data)
sheet.data.to_excel("{}一个月{}.xls".format("重庆", time.time())) # 只用于pd.dataframe sheet显示城市更改这里
|
免费评分
-
查看全部评分
本帖被以下淘专辑推荐:
- · 精品工具盒|主题: 2345, 订阅: 1052
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|