爬取天气数据并解析温度值

绝地灬酷狼 2023-03-04 15:26 80阅读 0赞

一、概述

获取北京周边城区的天气数据,链接如下:http://www.weather.com.cn/weather1d/101010100.shtml#input

format_png

最终需要得到以下数据:

  1. [
  2. {
  3. 'location': '香河', 'high': '36', 'low': '23°C'},
  4. ...
  5. ]

二、分析页面

地区

可以发现数据在 id=”around”这个div里面,地区的值在a标签中。

format_png 1

那么xpath规则为:

  1. //*[@id="around"]//a[@target="_blank"]/span/text()

效果如下:

format_png 2

温度

温度也是在同一个div里面,温度的值在i标签中

format_png 3

那么xpath规则为:

  1. //*[@id="around"]/div/ul/li/a/i/text()

效果如下:

format_png 4

三、完整代码

aHR0cHM6Ly9pbWFnZXMuY25ibG9ncy5jb20vT3V0bGluaW5nSW5kaWNhdG9ycy9Db250cmFjdGVkQmxvY2suZ2lm aHR0cHM6Ly9pbWFnZXMuY25ibG9ncy5jb20vT3V0bGluaW5nSW5kaWNhdG9ycy9FeHBhbmRlZEJsb2NrU3RhcnQuZ2lm

  1. import requests
  2. from lxml import etree
  3. url = 'http://www.weather.com.cn/weather1d/101010100.shtml#input'
  4. with requests.get(url) as res:
  5. content = res.content
  6. html = etree.HTML(content)
  7. location = html.xpath('//*[@id="around"]//a[@target="_blank"]/span/text()')
  8. temperature = html.xpath('//*[@id="around"]/div/ul/li/a/i/text()')
  9. data = dict(zip(location, temperature))
  10. # print(data,len(data))
  11. # 数据列表
  12. data_list = []
  13. for i in data:
  14. # 切割
  15. high,low = data[i].split('/')
  16. dic = {
  17. 'location':i,'high':high,'low':low}
  18. data_list.append(dic)
  19. print(data_list)

执行输出:

aHR0cHM6Ly9pbWFnZXMuY25ibG9ncy5jb20vT3V0bGluaW5nSW5kaWNhdG9ycy9Db250cmFjdGVkQmxvY2suZ2lm aHR0cHM6Ly9pbWFnZXMuY25ibG9ncy5jb20vT3V0bGluaW5nSW5kaWNhdG9ycy9FeHBhbmRlZEJsb2NrU3RhcnQuZ2lm

  1. [{
  2. 'location': '香河', 'high': '36', 'low': '23°C'}, {
  3. 'location': '涿州', 'high': '36', 'low': '25°C'}, {
  4. 'location': '唐山', 'high': '34', 'low': '24°C'}, {
  5. 'location': '沧州', 'high': '33', 'low': '26°C'}, {
  6. 'location': '天津', 'high': '34', 'low': '27°C'}, {
  7. 'location': '廊坊', 'high': '36', 'low': '24°C'}, {
  8. 'location': '太原', 'high': '32', 'low': '23°C'}, {
  9. 'location': '石家庄', 'high': '34', 'low': '26°C'}, {
  10. 'location': '涿鹿', 'high': '32', 'low': '20°C'}, {
  11. 'location': '张家口', 'high': '30', 'low': '17°C'}, {
  12. 'location': '保定', 'high': '36', 'low': '24°C'}, {
  13. 'location': '三河', 'high': '35', 'low': '23°C'}, {
  14. 'location': '北京孔庙', 'high': '37', 'low': '23°C'}, {
  15. 'location': '北京国子监', 'high': '37', 'low': '23°C'}, {
  16. 'location': '中国地质博物馆', 'high': '37', 'low': '23°C'}, {
  17. 'location': '月坛公园', 'high': '37', 'low': '22°C'}, {
  18. 'location': '明城墙遗址公园', 'high': '37', 'low': '23°C'}, {
  19. 'location': '北京市规划展览馆', 'high': '35', 'low': '24°C'}, {
  20. 'location': '什刹海', 'high': '37', 'low': '22°C'}, {
  21. 'location': '南锣鼓巷', 'high': '37', 'low': '23°C'}, {
  22. 'location': '天坛公园', 'high': '35', 'low': '24°C'}, {
  23. 'location': '北海公园', 'high': '35', 'low': '24°C'}, {
  24. 'location': '景山公园', 'high': '35', 'low': '24°C'}, {
  25. 'location': '北京海洋馆', 'high': '37', 'low': '23°C'}]

注意:这里2个列表转换为一个字典,使用了zip()函数。

本文参考链接:

https://github.com/jackzhenguo/python-small-examples/

发表评论

表情:
评论列表 (有 0 条评论,80人围观)

还没有评论,来说两句吧...

相关阅读