随着人工智能和大数据的发展,本来就风头强劲的Python在近几年更是发展得如日中天。众所周知,Python的应用场景十分广泛,像是在科学计算、游戏开发、web开发框架等方面都发挥了极大的作用,因此夸张一点说,掌握了Python你就无所不能了!下面我们就来看看如何用Python爬取天气数据并解析温度值~
爬取的html 结构
import requests
from lxml import etree
import pandas as pd
import re
url = 'http://www.weather.com.cn/weather1d/101010100.shtml#input'
with requests.get(url) as res:
content = res.content
html = etree.HTML(content)
通过lxml模块提取值
lxml比beautifulsoup解析在某些场合更高效
location = html.xpath('//*[@id="around"]//a[@target="_blank"]/span/text()')