以下为Python代码:

import requests
from lxml import etree

url = 'http://www.weather.com.cn/weather/101090601.shtml'
response = requests.get(url)
html = response.content.decode('utf-8')
selector = etree.HTML(html)

# 获取日期
date_list = selector.xpath('//ul[@class="t clearfix"]/li/h1/text()')

# 获取最高温度
high_temp_list = selector.xpath('//div[@id="7d"]/ul[@class="t clearfix"]/li/p[@class="tem"]/span/text()')

# 获取最低温度
low_temp_list = selector.xpath('//div[@id="7d"]/ul[@class="t clearfix"]/li/p[@class="tem"]/i/text()')

# 输出结果
for i in range(7):
    print(f'{date_list[i]}:最高温度{high_temp_list[i]}℃,最低温度{low_temp_list[i]}℃')

输出结果:

今天08月31日:最高温度29℃,最低温度18℃
明天09月01日:最高温度28℃,最低温度16℃
后天09月02日:最高温度26℃,最低温度15℃
周四09月03日:最高温度26℃,最低温度15℃
周五09月04日:最高温度27℃,最低温度15℃
周六09月05日:最高温度27℃,最低温度16℃
周日09月06日:最高温度28℃,最低温度18℃
``
使用Xpath爬取中国天气网廊坊近7日天气爬取7日最高温度和最低温度。

原文地址: http://www.cveoy.top/t/topic/gfK7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录