爬虫_中国天气网_文字天气预报(xpath)
- 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)的更多相关文章
- 微信小程序_(案例)简单中国天气网首页
Demo:简单中国天气网首页 Page({ data:{ name:"CynicalGary", temp:"4", low:"-1°C", ...
- 爬虫-通过本地IP地址从中国天气网爬取当前城市天气情况
1.问题描述 最近在做一个pyqt登录校园网的小项目,想在窗口的状态栏加上当天的天气情况,用爬虫可以很好的解决我的问题. 2.解决思路 考虑到所处位置的不同,需要先获取本地城市地址,然后作为中 ...
- 轻奢当道业绩逆势增长 Kate Spade联手韩国衣恋开拓中国市场_商场报道_中国时尚品牌网
轻奢当道业绩逆势增长 Kate Spade联手韩国衣恋开拓中国市场_商场报道_中国时尚品牌网 轻奢当道业绩逆势增长 Kate Spade联手韩国衣恋开拓中国市场
- 孙弘与Masa Maso 做互联网最贵的衬衫(2)_人物对话_中国时尚品牌网
孙弘与Masa Maso 做互联网最贵的衬衫(2)_人物对话_中国时尚品牌网 孙弘与Masa Maso 做互联网最贵的衬衫(2)
- 中国天气网-天气预报接口api
中国天气网地址:http://www.weather.com.cn 请求服务 : 查询实时天气信息 http://www.weather.com.cn/data/sk/101110101.html 在 ...
- 天气预报接口api(中国天气网)
中国天气weather.comhttp://m.weather.com.cn/data/101110101.html(六天预报) http://www.weather.com.cn/data/sk/1 ...
- C#获取中国天气网免费天气预报信息
中国天气网接口地址:”http://wthrcdn.etouch.cn/WeatherApi?citykey=” + weatherCityCode(为城市code); 下面是转化过程中我们需要用到的 ...
- 初识python 之 爬虫:爬取中国天气网数据
用到模块: 获取网页并解析:import requests,html5lib from bs4 import BeautifulSoup 使用pyecharts的Bar可视化工具"绘制图表& ...
- 中国天气网API接口
http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data/cityinfo/101010100.h ...
随机推荐
- H5 56-还原字体和字号
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- codeforces#1097 D. Makoto and a Blackboard(dp+期望)
题意:现在有一个数写在黑板上,它以等概率转化为它的一个约数,可以是1,问经过k次转化后这个数的期望值 题解:如果这个数是一个素数的n次方,那么显然可以用动态规划来求这个数的答案,否则的话,就对每个素因 ...
- 1168: mxh对lfx的询问(前缀和+素数表)
题目描述: AS WE ALL KNOW, lfx是咱们组的神仙,但是mxh想考一考lfx一个简单的问题,以此看一下lfx到底是不是神仙.但是lfx要准备补考,于是请你来帮忙回答问题: 给定一个整数N ...
- [options] 未与 -source 1.6 一起设置引导类路径
用ant与eclipse编译Cassandra 1.2.19,出现了“ [options] 未与 -source1.6一起设置引导类路径”的警告,并出现了一些编译错误,提示编译失败,上网找了很 多资料 ...
- bnu——GCD SUM (莫比乌斯反演)
题目:GCD SUM 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=39872 算法:莫比乌斯反演.优化 #include<stdio.h& ...
- C# Note11:如何优雅地退出WPF应用程序
前言 I should know how I am supposed to exit my application when the user clicks on the Exit menu item ...
- git客户端下载 和安装
网址 https://git-scm.com/download/win 点击next 说明: (1)图标组件(Addition icons) : 选择是否创建桌面快捷方式. (2)桌面浏览(Wind ...
- python之路--MySQL多表查询
一 介绍 我们在写项目的时候一般都会建一个数据库,数据库里面会存很多的表,不可能把所有的数据都放在一张表里,因为分表来存数据节省空间,数据的组织结构更清晰,解耦和程度更高,但是这些表本质上还不是一个整 ...
- final关键字的用法
final关键字的作用 1.被final修饰的类不能被继承 报错信息:cannot inherit from final 'com.dajia.test.Animal' 2.被final修饰的方法不能 ...
- python设计模式第二十四天【命令模式】
1.使用场景 (1)调用过程比较繁琐,需要封装 (2)调用参数需要进行处理封装 (3)需要添加额外的功能,例如,日志,缓存,操作记录等 2.代码实现 #!/usr/bin/env python #! ...