一个简单的获取天气例子

import requests
from lxml import etree

headers={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}
url = 'http://www.weather.com.cn/weather/101270101.shtml'
res = requests.get(url,headers=headers,verify=False)
html = etree.HTML(res.content.decode('utf-8'))
infos = html.xpath('//div[@id="7d"]/ul/li/h1/text()')#自己编写的xpath
temps = html.xpath('//div[@id="7d"]/ul/li/p/@title')
#将日期和对应的天气转换为json
for i in range(len(infos)):
    data = {
        'day':infos[i],
        'weather':temps[i]
    }
    print(data)

猜你喜欢

转载自blog.csdn.net/u014229742/article/details/84331636