xpath爬取新浪天气
参考资料:
http://cuiqingcai.com/1052.html
http://cuiqingcai.com/2621.html
http://www.cnblogs.com/jixin/p/5131040.html
完整代码:
# -*- coding:utf-8 -*-
import urllib2
from lxml import etree
user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"
headers = {'User-Agent':user_agent}
url = 'http://weather.sina.com.cn/'
user_input = raw_input("请输入你想查询天气的城市的拼音,如beijing\n")
# print user_input
url = url+user_input
# print url
req = urllib2.Request(url,headers=headers)
reponse = urllib2.urlopen(req)
text = reponse.read()
# print html
# print type(text)
html = etree.HTML(text)
# print html
# result = etree.tostring(html)
# print result
# 有时候当天天气信息的icons和times数据只有19条,分开处理
def change_list(lis):
new_lis = []
if len(lis) == 19:
if lis == icons:
new_lis.append(lis[0])
for i in range(1,19,2):
new_lis.append(lis[i]+u'转'+lis[i+1])
elif lis == times:
new_lis.append(lis[0].text)
for i in range(1, 19, 2):
new_lis.append(lis[i].text + u'到' + lis[i + 1].text)
elif len(lis) == 20:
if lis == icons:
for i in range(20,2):
new_lis.append(lis[i]+u'转'+lis[i+1])
elif lis == times:
for i in range(20,2):
new_lis.append(lis[i].text + u'到' + lis[i + 1].text)
return new_lis
note1 = html.xpath('//*[@class="wt_tt0_note"]')
note2 = html.xpath('//*[@class="wt_tt0_note"]/..')
# print note[0].text
dates = html.xpath('//*[@class="wt_fc_c0_i_date"]')
days = html.xpath('//*[@class="wt_fc_c0_i_date"]/following-sibling::*[1]')
icons = html.xpath('//*[@class="wt_fc_c0_i_icons clearfix"]/img/@alt')
# print len(icons)
icons = change_list(icons)
times = html.xpath('//*[@class="wt_fc_c0_i_times"]/span')
times = change_list(times)
temps = html.xpath('//*[@class="wt_fc_c0_i_temp"]')
tips = html.xpath('//*[@class="wt_fc_c0_i_tip"]')
ls = html.xpath('//*[@class="l"]')
rs = html.xpath('//*[@class="r"]')
print note1[0].text,note2[0].text
# print len(ls),len(rs)
# PM2.5和空气质量只有7条数据
for i in range(7):
print dates[i].text, days[i].text, times[i], icons[i], temps[i].text, tips[i].text, u'PM2.5:'+ls[i].text, u'空气质量:'+rs[i].text
for i in range(7,10):
print dates[i].text, days[i].text, times[i], icons[i], temps[i].text, tips[i].text
xpath爬取新浪天气的更多相关文章
- selenium+BeautifulSoup+phantomjs爬取新浪新闻
一 下载phantomjs,把phantomjs.exe的文件路径加到环境变量中,也可以phantomjs.exe拷贝到一个已存在的环境变量路径中,比如我用的anaconda,我把phantomjs. ...
- Python3:爬取新浪、网易、今日头条、UC四大网站新闻标题及内容
Python3:爬取新浪.网易.今日头条.UC四大网站新闻标题及内容 以爬取相应网站的社会新闻内容为例: 一.新浪: 新浪网的新闻比较好爬取,我是用BeautifulSoup直接解析的,它并没有使用J ...
- Python 爬虫实例(7)—— 爬取 新浪军事新闻
我们打开新浪新闻,看到页面如下,首先去爬取一级 url,图片中蓝色圆圈部分 第二zh张图片,显示需要分页, 源代码: # coding:utf-8 import json import redis i ...
- python3爬虫-爬取新浪新闻首页所有新闻标题
准备工作:安装requests和BeautifulSoup4.打开cmd,输入如下命令 pip install requests pip install BeautifulSoup4 打开我们要爬取的 ...
- python3使用requests爬取新浪热门微博
微博登录的实现代码来源:https://gist.github.com/mrluanma/3621775 相关环境 使用的python3.4,发现配置好环境后可以直接使用pip easy_instal ...
- python2.7 爬虫初体验爬取新浪国内新闻_20161130
python2.7 爬虫初学习 模块:BeautifulSoup requests 1.获取新浪国内新闻标题 2.获取新闻url 3.还没想好,想法是把第2步的url 获取到下载网页源代码 再去分析源 ...
- python爬取新浪股票数据—绘图【原创分享】
目标:不做蜡烛图,只用折线图绘图,绘出四条线之间的关系. 注:未使用接口,仅爬虫学习,不做任何违法操作. """ 新浪财经,爬取历史股票数据 ""&q ...
- 【python3】爬取新浪的栏目分类
目标地址: http://www.sina.com.cn/ 查看源代码,分析: 1 整个分类 在 div main-nav 里边包含 2 分组情况:1,4一组 . 2,3一组 . 5 一组 .6一组 ...
- Python-定时爬取指定城市天气(一)-发送给关心的微信好友
一.背景 上班的日子总是3点一线,家里,公司和上班的路径,对于一个特别懒得我来说,经常遇到上班路上下雨了,而我却没带伞,多么痛的领悟.最近对python有一种狂热的学习热情,写了4年多的C++代码,对 ...
随机推荐
- innodb 关键特性(两次写与自适应哈希索引)
两次写: 场景: 当发生数据库宕机时,可能innodb存储引擎正在写入某个页到表中,而这个页只写了一部分,这种情况被称为部分写失效,如果发生,可以通过重做日志进行恢复,重做日志中记录的是对页的物理操作 ...
- The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored(E F G H I)
http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=392 E:Sequence in the Pocket 思路:从 ...
- Linux下普通IO文件操作函数---C语言
普通文件IO总结 FILE结构体 typedef struct { int level; /*填充/清空一级缓存*/ unsigned flag; /*文件状态指针*/ ...
- easyUI添加修改tab页(toolbar)
代码: <div id="editdialos" class="easyui-dialog" title="虚机配置修改" data- ...
- 关于easyui框架中a标签使用onclick()触发事件偶尔会选项卡消失BUG解决方案
今天发现公司的一个easyui项目中有个页面会在触发onclick事件时选项卡消失,如下图 产生BUG后 产生BUG前 查找很多地方还有资料不知道哪里出现的问题,看了下框架源码之类的,因为不是专门的前 ...
- Java并发编程:深入剖析ThreadLocal(转)
目录大纲: 一.对ThreadLocal的理解 二.深入解析ThreadLocal类 三.ThreadLocal的应用场景 原文链接:http://www.cnblogs.com/dolphin052 ...
- thinkphp5实现多级控制器
默认情况下目录结构 application ->admin->controller->class.php 当项目比较多的时候,目录下控制器文件较多,考虑按模块增加一层目录 appli ...
- Git实际操作
1.基本操作 git init 初始化仓库 git status 查看仓库状态 git add XXX.XX 向暂存区中添加文件XXX.XX git commit 保存仓库的历史记录 git log ...
- C#使用反射获取对象变化的情况
记录日志时, 经常需要描述对象的状态发生了怎样的变化, 以前处理的非常简单粗暴: a. 重写class的ToString()方法, 将重要的属性都输出来 b. 记录日志时: 谁谁谁 由 变更前实 ...
- 分析easyswoole3.0源码,consoleTcpService(六)
前文讲过可以通过配置开启一个tcp服务,叫做consoleTcpservice.EasySwoole\EasySwoole\Core::83行 (new TcpService(Config::getI ...