一個簡單的獲取天氣例子
阿新 • • 發佈:2018-12-06
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)