1. import requests
  2. from lxml import etree
  3.  
  4. headers = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
  5.  
  6. def get_html(url):
  7. try:
  8. html = requests.get(url, headers={'User-Agent':'headers'})
  9. html.encoding = html.apparent_encoding
  10. if html.status_code == 200:
  11. return html.text
  12. return 0
  13.  
  14. except RequestsException:
  15. return 0
  16. '''
  17. conMintab: 华北
  18. conMintab2 北京
  19. conMintab2 天津
  20. conMintab2 河北
  21. conMintab2 山西
  22. conMintab2 内蒙古
  23.  
  24. //div[@class="conMidtab"][1] 今天
  25. //div[@class="conMidtab"][2] 明天
  26. ...
  27. //div[@class="conMidtab"][7] 最后一天
  28. '''
  29.  
  30. def parse_html(html):
  31. wea = []
  32. html_element = etree.HTML(html)
  33. # !!!!! trs = html_element.xpath('//div[@class="conMidtab"][1]//tr')[2:]
  34. provinces = html_element.xpath('//div[@class="conMidtab"][1]//div[@class="conMidtab2"]')
  35. for province in provinces:
  36. trs = province.xpath('.//tr')[2:]
  37. for tr in trs:
  38. weather = {}
  39. city = tr.xpath('.//td[@width="83"]/a/text()')
  40. phenomenon = tr.xpath('.//td[@width="89"]/text()')
  41. wind = tr.xpath('.//td[@width="162"]//text()')
  42. hightest = tr.xpath('.//td[@width="92"]/text()')
  43. weather['city'] = city
  44. weather['phenomenon'] = phenomenon
  45. weather['wind'] = wind
  46. weather['hightest'] = hightest
  47. while '\n' in wind:
  48. wind.remove('\n')
  49. wea.append(weather)
  50.  
  51. print(wea)
  52.  
  53. def main():
  54. urls = ['http://www.weather.com.cn/textFC/hb.shtml',
  55. 'http://www.weather.com.cn/textFC/db.shtml',
  56. 'http://www.weather.com.cn/textFC/hd.shtml',
  57. 'http://www.weather.com.cn/textFC/hz.shtml',
  58. 'http://www.weather.com.cn/textFC/hn.shtml',
  59. 'http://www.weather.com.cn/textFC/xb.shtml',
  60. 'http://www.weather.com.cn/textFC/xn.shtml',
  61. 'http://www.weather.com.cn/textFC/gat.shtml']
  62. for url in urls:
  63. html = get_html(url)
  64. if html == 0:
  65. html = get_html(url)
  66. parse_html(html)
  67.  
  68. if __name__ == '__main__':
  69. main()

。。港澳台的格式不太一样,暂时不想管他们了

。。运行结果的话,我爬取得是当日白天的天气,现在晚上了,数据都没有了(一开始还以为是代码改错了,还一直撤销)

爬虫_中国天气网_文字天气预报(xpath)的更多相关文章

  1. 微信小程序_(案例)简单中国天气网首页

    Demo:简单中国天气网首页 Page({ data:{ name:"CynicalGary", temp:"4", low:"-1°C", ...

  2. 爬虫-通过本地IP地址从中国天气网爬取当前城市天气情况

    1.问题描述 ​ 最近在做一个pyqt登录校园网的小项目,想在窗口的状态栏加上当天的天气情况,用爬虫可以很好的解决我的问题. 2.解决思路 ​ 考虑到所处位置的不同,需要先获取本地城市地址,然后作为中 ...

  3. 轻奢当道业绩逆势增长 Kate Spade联手韩国衣恋开拓中国市场_商场报道_中国时尚品牌网

    轻奢当道业绩逆势增长 Kate Spade联手韩国衣恋开拓中国市场_商场报道_中国时尚品牌网 轻奢当道业绩逆势增长 Kate Spade联手韩国衣恋开拓中国市场

  4. 孙弘与Masa Maso 做互联网最贵的衬衫(2)_人物对话_中国时尚品牌网

    孙弘与Masa Maso 做互联网最贵的衬衫(2)_人物对话_中国时尚品牌网 孙弘与Masa Maso 做互联网最贵的衬衫(2)

  5. 中国天气网-天气预报接口api

    中国天气网地址:http://www.weather.com.cn 请求服务 : 查询实时天气信息 http://www.weather.com.cn/data/sk/101110101.html 在 ...

  6. 天气预报接口api(中国天气网)

    中国天气weather.comhttp://m.weather.com.cn/data/101110101.html(六天预报) http://www.weather.com.cn/data/sk/1 ...

  7. C#获取中国天气网免费天气预报信息

    中国天气网接口地址:”http://wthrcdn.etouch.cn/WeatherApi?citykey=” + weatherCityCode(为城市code); 下面是转化过程中我们需要用到的 ...

  8. 初识python 之 爬虫:爬取中国天气网数据

    用到模块: 获取网页并解析:import requests,html5lib from bs4 import BeautifulSoup 使用pyecharts的Bar可视化工具"绘制图表& ...

  9. 中国天气网API接口

    http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data/cityinfo/101010100.h ...

随机推荐

  1. H5 56-还原字体和字号

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. codeforces#1097 D. Makoto and a Blackboard(dp+期望)

    题意:现在有一个数写在黑板上,它以等概率转化为它的一个约数,可以是1,问经过k次转化后这个数的期望值 题解:如果这个数是一个素数的n次方,那么显然可以用动态规划来求这个数的答案,否则的话,就对每个素因 ...

  3. 1168: mxh对lfx的询问(前缀和+素数表)

    题目描述: AS WE ALL KNOW, lfx是咱们组的神仙,但是mxh想考一考lfx一个简单的问题,以此看一下lfx到底是不是神仙.但是lfx要准备补考,于是请你来帮忙回答问题: 给定一个整数N ...

  4. [options] 未与 -source 1.6 一起设置引导类路径

    用ant与eclipse编译Cassandra 1.2.19,出现了“ [options] 未与 -source1.6一起设置引导类路径”的警告,并出现了一些编译错误,提示编译失败,上网找了很 多资料 ...

  5. bnu——GCD SUM (莫比乌斯反演)

    题目:GCD SUM 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=39872 算法:莫比乌斯反演.优化 #include<stdio.h& ...

  6. C# Note11:如何优雅地退出WPF应用程序

    前言 I should know how I am supposed to exit my application when the user clicks on the Exit menu item ...

  7. git客户端下载 和安装

    网址  https://git-scm.com/download/win 点击next 说明: (1)图标组件(Addition icons) : 选择是否创建桌面快捷方式. (2)桌面浏览(Wind ...

  8. python之路--MySQL多表查询

    一 介绍 我们在写项目的时候一般都会建一个数据库,数据库里面会存很多的表,不可能把所有的数据都放在一张表里,因为分表来存数据节省空间,数据的组织结构更清晰,解耦和程度更高,但是这些表本质上还不是一个整 ...

  9. final关键字的用法

    final关键字的作用 1.被final修饰的类不能被继承 报错信息:cannot inherit from final 'com.dajia.test.Animal' 2.被final修饰的方法不能 ...

  10. python设计模式第二十四天【命令模式】

    1.使用场景 (1)调用过程比较繁琐,需要封装 (2)调用参数需要进行处理封装 (3)需要添加额外的功能,例如,日志,缓存,操作记录等 2.代码实现 #!/usr/bin/env python #! ...