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()





你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()