好友
阅读权限 10
听众
最后登录 1970-1-1
本帖最后由 luck20121221 于 2022-11-30 10:08 编辑
#上一篇是抓历史天气这一篇是未来一周天气。结构差不多前一篇有注释
import requests
from bs4 import BeautifulSoup
import xlwt
import time
headers = {
'User-Agent' : 'Mozilla/5.0' }
url = "http://www.weather.com.cn/weather/101270101.shtml"
weather_str = requests.get(url, headers =headers)
weather_str_content = weather_str.content
# print(weather_str_content)
soup = BeautifulSoup(weather_str_content, 'html.parser' )
soup_ul = soup.find('ul' , "t clearfix" )
# print(soup_ul)
# 提取数据
soup_li_list = soup_ul.find_all("li" )
weather_list = []
for i in range (len (soup_li_list)):
# 天气
weather_data = soup_li_list.find("h1" ).get_text()
# print(weather_data)
# 天气
weather_wea = soup_li_list.find("p" , "wea" ).get_text()
# 温度
if soup_li_list.find("p" , "tem" ).find("span" ):
max_tem = soup_li_list.find("p" , "tem" ).find("span" ).get_text()
else :
max_tem = soup_li_list.find("p" , "tem" ).find("i" ).get_text()
min_tem = soup_li_list.find('p' , "tem" ).find("i" ).get_text()
# print(" 最高温度 :",max_tem)
# print(" 最低温度 :",min_tem)
# 风向
soup_win = soup_li_list.find("p" , "win" ).find_all('span' )
soup_win1 = soup_win[0 ].get("title" )
try :
soup_win2 = soup_win[1 ].get("title" ).get_text()
except :
soup_win2 = soup_win1
# 风力
win_i = soup_li_list.find("p" , "win" ).find('i' ).get_text()
weather_list.append([weather_data, weather_wea, max_tem, min_tem, soup_win1, soup_win2, win_i])
workbook = xlwt.Workbook()
sheet = workbook.add_sheet("chengdu" )
sheet.write(0 , 0 , " 日期 " )
sheet.write(0 , 1 , " 天气 " )
sheet.write(0 , 2 , " 最高温度 " )
sheet.write(0 , 3 , " 最低温度 " )
sheet.write(0 , 4 , " 风向 1" )
sheet.write(0 , 5 , " 风向 2" )
sheet.write(0 , 6 , " 风力 " )
for i in range (len (weather_list)):
sheet.write(i + 1 , 0 , weather_list[0 ])
sheet.write(i + 1 , 1 , weather_list[1 ])
sheet.write(i + 1 , 2 , weather_list[2 ])
sheet.write(i + 1 , 3 , weather_list[3 ])
sheet.write(i + 1 , 4 , weather_list[4 ])
sheet.write(i + 1 , 5 , weather_list[5 ])
sheet.write(i + 1 , 6 , weather_list[6 ])
workbook.save("{}7 天天气 {}.xls" .format("chengdu" , time.time()))
免费评分
查看全部评分