import requests
from lxml import etree headers = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' def get_html(url):
try:
html = requests.get(url, headers={'User-Agent':'headers'})
html.encoding = html.apparent_encoding
if html.status_code == 200:
return html.text
return 0 except RequestsException:
return 0
'''
conMintab: 华北
conMintab2 北京
conMintab2 天津
conMintab2 河北
conMintab2 山西
conMintab2 内蒙古 //div[@class="conMidtab"][1] 今天
//div[@class="conMidtab"][2] 明天
...
//div[@class="conMidtab"][7] 最后一天
''' def parse_html(html):
wea = []
html_element = etree.HTML(html)
# !!!!! trs = html_element.xpath('//div[@class="conMidtab"][1]//tr')[2:]
provinces = html_element.xpath('//div[@class="conMidtab"][1]//div[@class="conMidtab2"]')
for province in provinces:
trs = province.xpath('.//tr')[2:]
for tr in trs:
weather = {}
city = tr.xpath('.//td[@width="83"]/a/text()')
phenomenon = tr.xpath('.//td[@width="89"]/text()')
wind = tr.xpath('.//td[@width="162"]//text()')
hightest = tr.xpath('.//td[@width="92"]/text()')
weather['city'] = city
weather['phenomenon'] = phenomenon
weather['wind'] = wind
weather['hightest'] = hightest
while '\n' in wind:
wind.remove('\n')
wea.append(weather) print(wea) def main():
urls = ['http://www.weather.com.cn/textFC/hb.shtml',
'http://www.weather.com.cn/textFC/db.shtml',
'http://www.weather.com.cn/textFC/hd.shtml',
'http://www.weather.com.cn/textFC/hz.shtml',
'http://www.weather.com.cn/textFC/hn.shtml',
'http://www.weather.com.cn/textFC/xb.shtml',
'http://www.weather.com.cn/textFC/xn.shtml',
'http://www.weather.com.cn/textFC/gat.shtml']
for url in urls:
html = get_html(url)
if html == 0:
html = get_html(url)
parse_html(html) if __name__ == '__main__':
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. super关键字访问父类成员

    1.super只能出现在子类的方法和构造方法中: 2.super调用构造方法时只能是第一句: 3.super不能访问父类的private成员.

  2. Python—time模块介绍

    time 模块 在平常的代码中,我们常常需要与时间打交道.在Python中,常用的与时间处理有关的模块就包括:time,datetime,下面来介绍time模块. 在开始之前,首先要说明几点: 一.在 ...

  3. Python IO模型

    这篇博客是本人借鉴一些大神的博客并结合自己的学习过程写下的. 事件驱动模型 事件驱动模型是一种编程范式,这里程序的执行流由外部事件来决定.它的特点是包含一个事件循环,当外部事件发生时,不断从队列里取出 ...

  4. Echarts x轴文本内容太长的几种解决方案

    Echarts 标签中文本内容太长的时候怎么办 ? - 1对文本进行倾斜 在xAxis.axisLabe中修改rotate的值 xAxis: { data: ["衬衫11111", ...

  5. stark组件的增删改(新)

      1.效果图 2.详细步骤解析 3.总结.代码   1.效果图 增 删除 改 2.详细步骤解析 1.构造增删改查url,反向解析 2.ModelForm定制add.edit页面 3.starak中的 ...

  6. 05Hadoop 概论

    Hadoop的思想之源:Google Google搜索引擎,Gmail,安卓,AppspotGoogle Maps,Google earth,Google 学术,Google翻译,Google+,下一 ...

  7. CGI、FAST-CGI、PHP-CGI、PHP-FPM的关系

    转自:https://www.awaimai.com/371.html 关于这一类的文章还有:https://zhuanlan.zhihu.com/p/20694204 在搭建 LAMP/LNMP 服 ...

  8. Nginx负载均衡各种配置方式

    Nginx负载均衡 - 小刚qq - 博客园http://www.cnblogs.com/xiaogangqq123/archive/2011/03/04/1971002.html Module ng ...

  9. Azure系列2.1.4 —— BlobInputStream

    (小弟自学Azure,文中有不正确之处,请路过各位大神指正.) 网上azure的资料较少,尤其是API,全是英文的,中文资料更是少之又少.这次由于公司项目需要使用Azure,所以对Azure的一些学习 ...

  10. 相识mongodb

    1.下载完安装包,并解压下载地址:https://www.mongodb.org/dl/linux/x86_64或者可以直接wget http://fastdl.mongodb.org/linux/m ...