Life_is_Romantic 2021-07-29 14:46 采纳率: 68.4%
浏览 49
已结题

python爬取天气预报的信息程序不报错但是无法显示打印的结果

img

循坏里面就打印不出结果是怎么回事呢?

img

代码如下:想要打印城市,天气等信息

import requests
from lxml import etree

def get_weather():
    url = 'http://www.weather.com.cn/textFC/hb.shtml'
    headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.55'}
    response=requests.get(url,headers=headers)
    response.encoding='utf-8'
    html=response.text
    tree=etree.HTML(html)
    day=tree.xpath('/html/body/div[4]/div[2]/div/div/ul[2]/li[1]/text()')[0]
    print(day)
    tbody=tree.xpath('/html/body/div[4]/div[2]/div/div/div[2]/div[1]/div[1]/table/tbody/tr[3]')
    for index,item in enumerate(tbody):
        if index == 0:
            province_name = item.xpath('./td[1]/a/text()')[0]
            print(province_name)
            city_name = item.xpath('./td[2]/a/text()')[0]
            print(city_name)
            weather = item.xpath('./td[3]/text()')[0]
            print(weather)
            wind = item.xpath('./td[4]//text()')[0]
            print(wind)
            high = item.xpath('./td[5]/text()')[0]
            weather_night = item.xpath('./td[6]/text()')[0]
            wind_night = item.xpath('./td[7]//text()')[0]
            low = item.xpath('./td[8]/text()')[0]
            #print(province_name)
        else:
            city_name=item.xpath('./td[1]/text()')[0]
            weather = item.xpath('./td[2]/text()')[0]
            wind = item.xpath('./td[3]//text()')[0]
            high = item.xpath('./td[4]/text()')[0]
            weather_night = item.xpath('./td[5]/text()')[0]
            wind_night = item.xpath('./td[6]//text()')[0]
            low = item.xpath('./td[7]/text()')[0]
            print(city_name, weather, wind, high, weather_night, wind_night, low)
if __name__ == '__main__':
    get_weather()





  • 写回答

1条回答 默认 最新

  • 八云黧 2021-07-29 14:53
    关注

    你debug一遍不就知道了,tbody是空的list,你遍历不了
    好像和tbody有关,去掉tbody就有一些数据了
    改了一下代码,你看一下

    import requests
    from lxml import etree
    def get_weather():
        url = 'http://www.weather.com.cn/textFC/hb.shtml'
        headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.55'}
        response=requests.get(url,headers=headers)
        response.encoding='utf-8'
        html=response.text
        tree=etree.HTML(html)
        day=tree.xpath('/html/body/div[4]/div[2]/div/div/ul[2]/li[1]/text()')[0]
        print(day)
        table=tree.xpath('/html/body/div[4]/div[2]/div/div/div[2]/div[1]/div[1]/table/tr')
        province_name = table[2].xpath('./td[1]/a/text()')[0]
        for index,item in enumerate(table):
            if index < 2:
                continue
            if index == 2:
                print(province_name,end="\t|")
                city_name = item.xpath('./td[2]/a/text()')[0]
                print(city_name,end="\t|")
                weather = item.xpath('./td[3]/text()')[0]
                print(weather,end="\t|")
                wind = item.xpath('./td[4]/span[1]/text()')[0] + '\t' + item.xpath('./td[4]/span[2]/text()')[0]
                print(wind,end="|")
                high = item.xpath('./td[5]/text()')[0]
                print(high,end="\t|")
                weather_night = item.xpath('./td[6]/text()')[0]
                print(weather_night,end="\t|")
                wind_night = item.xpath('./td[7]/span[1]/text()')[0] + '\t' + item.xpath('./td[7]/span[2]/text()')[0]
                print(wind_night,end="|")
                low = item.xpath('./td[8]/text()')[0]
                print(low)
            else:
                city_name = item.xpath('./td[1]/a/text()')[0]
                print('\t|'+city_name,end="\t|")
                weather = item.xpath('./td[2]/text()')[0]
                print(weather,end="\t|")
                wind = item.xpath('./td[3]/span[1]/text()')[0] + '\t' + item.xpath('./td[3]/span[2]/text()')[0]
                print(wind,end="|")
                high = item.xpath('./td[4]/text()')[0]
                print(high,end="\t|")
                weather_night = item.xpath('./td[5]/text()')[0]
                print(weather_night,end="\t|")
                wind_night = item.xpath('./td[6]/span[1]/text()')[0] + '\t' + item.xpath('./td[6]/span[2]/text()')[0]
                print(wind_night,end="|")
                low = item.xpath('./td[7]/text()')[0]
                print(low)
    if __name__ == '__main__':
        get_weather()
     
     
     
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月9日
  • 已采纳回答 9月1日
  • 创建了问题 7月29日

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据