吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 964|回复: 8
收起左侧

[讨论] python抓取未来天气

[复制链接]
luck20121221 发表于 2022-11-30 10:07
本帖最后由 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()))

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
zz1181 + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

帅小怪 发表于 2022-11-30 11:14
  File "/workspace/PythonProject/练习题/天气2.py", line 20, in <module>
    weather_data = soup_li_list.find("h1").get_text()
  File "/home/user/.local/lib/python3.10/site-packages/bs4/element.py", line 2289, in __getattr__
    raise AttributeError(
AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
zz1181 发表于 2022-11-30 12:47
littleworm 发表于 2022-11-30 13:17
hjxhjxjx 发表于 2022-11-30 13:19
帅小怪 发表于 2022-11-30 11:14
File "/workspace/PythonProject/练习题/天气2.py", line 20, in
    weather_data = soup_li_list.fin ...

把第16行的“find_all”改为“find”,就可以运行了
但结果好像有的问题
屏幕截图_20221130_131829.png
为什么表格一直都是这个数据?还是我自己的问题?
yingchengfeng 发表于 2022-11-30 14:19
谢谢分享
kll545012 发表于 2022-11-30 15:11
小雨 149°!!直接蒸发了吧
 楼主| luck20121221 发表于 2022-11-30 15:44
hjxhjxjx 发表于 2022-11-30 13:19
把第16行的“find_all”改为“find”,就可以运行了
但结果好像有的问题

#重新新建环境   可以下载pycharm
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()))
WZL1188888 发表于 2022-12-1 08:43
感谢分享,了解一下。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-10 10:23

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表