Python爬虫实例扒取2345天气预报


Posted in Python onMarch 04, 2018

寒假里学习了一下Python爬虫,使用最简单的方法扒取需要的天气数据,对,没听错,最简单的方法。甚至没有一个函数封装。。

网址:http://tianqi.2345.com/wea_history/53892.htm

火狐中右键查看网页源代码,没有发现天气数据,因此推断网页采用的json格式数据。

右击->查看元素->网络->JS,找到了位置

Python爬虫实例扒取2345天气预报

用Python爬虫下载为json格式数据存储下来,代码如下:

#-*- coding:utf-8 -*- 
import urllib2 
import json 
 
months = [1,2,3,4,5,6,7,8,9,10,11,12] 
years = [2011,2012,2013,2014,2015,2016] 
city = [53892]  #邯郸代码53892 
 
for y in years: 
  for m in months: 
    for c in city: 
      url = "http://tianqi.2345.com/t/wea_history/js/"+str(c)+"_"+str(y)+str(m)+".js?qq-pf-to=pcqq.c2c" 
      print url 
      html = urllib2.urlopen(url) 
      srcData = html.read() 
      #JsonData = json.loads(srcData) 
      file = open("d:/json/"+str(c)+"handan/weather"+str(c)+"_"+str(y)+str(m)+".json","w") 
      file.write(srcData) 
      file.close()
扒取存到本地:

因为是刚学,学一点就动手实践了一下,还没有学到json的转换,直接使用的正则匹配,提取json中的数据,直接打印

提取转换json文件中的数据Python代码:

#-*- coding:utf-8 -*- 
import json 
import re 
import time 
 
Year = [2014] 
Month = [1] 
 
for y in Year: 
  for m in Month: 
     
    """ 
    2016年2月15日终于改成功。 
    是因为正则匹配后的编码问题,导致输出时无法显示。 
    在每个正则匹配的元组后添加 .decode('gbk').encode('utf-8'),成功输出 
    """ 
    content = fRead.read() 
    pattern = re.compile('{ymd:\'(.*?)\',bWendu:\'(.*?)\',yWendu:\'(.*?)\',tianqi:\'(.*?)\',fengxiang:\'(.*?)\',fengli:\'(.*?)\'},',re.S) 
    items = re.findall(pattern,content) 
    for item in items: 
      print item[0].decode('gbk').encode('utf-8'),","+item[1].decode('gbk').encode('utf-8'),","+item[2].decode('gbk').encode('utf-8'),","+item[3].decode('gbk').encode('utf-8'),","+item[4].decode('gbk').encode('utf-8'),","+item[5].decode('gbk').encode('utf-8') 
      time.sleep(0.1) 
 
    fRead.close()

使用Sublime Text 3运行

使用正则处理的一大问题就是,格式不整齐,总会漏掉一些数据。可能是由于匹配的速度过快导致部分数据缺失,但是通过time.sleep() 睡眠依旧不能解决问题。

由此可以看出正则匹配时的缺陷,待以后使用Python中专门用于处理json数据的包以后,再重新试一下

Python 相关文章推荐
python使用reportlab画图示例(含中文汉字)
Dec 03 Python
Python的Django框架中使用SQLAlchemy操作数据库的教程
Jun 02 Python
举例讲解Python中字典的合并值相加与异或对比
Jun 04 Python
Python分治法定义与应用实例详解
Jul 28 Python
解决python3 HTMLTestRunner测试报告中文乱码的问题
Dec 17 Python
Python如何应用cx_Oracle获取oracle中的clob字段问题
Aug 27 Python
Docker部署Python爬虫项目的方法步骤
Jan 19 Python
PyCharm取消波浪线、下划线和中划线的实现
Mar 03 Python
Python如何安装第三方模块
May 28 Python
Python pymsql模块的使用
Sep 07 Python
使用pandas读取表格数据并进行单行数据拼接的详细教程
Mar 03 Python
浅谈Python数学建模之固定费用问题
Jun 23 Python
Python爬虫设置代理IP的方法(爬虫技巧)
Mar 04 #Python
浅析python实现scrapy定时执行爬虫
Mar 04 #Python
Python使用Scrapy爬虫框架全站爬取图片并保存本地的实现代码
Mar 04 #Python
Python爬虫框架Scrapy实例代码
Mar 04 #Python
详解python中asyncio模块
Mar 03 #Python
python3.6+django2.0开发一套学员管理系统
Mar 03 #Python
python爬虫面试宝典(常见问题)
Mar 02 #Python
You might like
PHP5与MySQL数据库操作常用代码 收集
2010/03/21 PHP
php stripslashes和addslashes的区别
2014/02/03 PHP
PHP实现冒泡排序的简单实例
2016/05/26 PHP
Laravel框架中VerifyCsrfToken报错问题的解决
2017/08/30 PHP
从ThinkPHP3.2.3过渡到ThinkPHP5.0学习笔记图文详解
2019/04/03 PHP
jQuery下通过replace字符串替换实现大小图片切换
2012/05/22 Javascript
jQuery的显示和隐藏方法与css隐藏的样式对比
2013/10/18 Javascript
使用js实现一个可编辑的select下拉列表
2014/02/20 Javascript
javascript工厂方式定义对象
2014/12/26 Javascript
js实现页面跳转的五种方法推荐
2016/03/10 Javascript
jQuery中通过ajax调用webservice传递数组参数的问题实例详解
2016/05/20 Javascript
jQuery根据表单name获取值的方法
2016/05/24 Javascript
原生js实现返回顶部缓冲效果
2017/01/18 Javascript
基于jQuery代码实现圆形菜单展开收缩效果
2017/02/13 Javascript
vue实现树形菜单效果
2018/03/19 Javascript
Vee-validate 父组件获取子组件表单校验结果的实例代码
2019/05/20 Javascript
javaScript中indexOf用法技巧
2019/11/26 Javascript
创建pycharm的自定义python模板方法
2018/05/23 Python
Django基础知识与基本应用入门教程
2018/07/20 Python
windows下 兼容Python2和Python3的解决方法
2018/12/05 Python
详解python执行shell脚本创建用户及相关操作
2019/04/11 Python
python multiprocessing多进程变量共享与加锁的实现
2019/10/02 Python
python 实现单通道转3通道
2019/12/03 Python
python-numpy-指数分布实例详解
2019/12/07 Python
python两个_多个字典合并相加的实例代码
2019/12/26 Python
python numpy矩阵信息说明,shape,size,dtype
2020/05/22 Python
基于opencv的selenium滑动验证码的实现
2020/07/24 Python
美国最大的无人机经销商:DroneNerds
2018/03/20 全球购物
Lime Crime官网:美国一家主打梦幻精灵系的彩妆品牌
2019/03/22 全球购物
办公室文秘自我评价
2013/09/21 职场文书
安全口号大全
2014/06/21 职场文书
作风建设年活动总结
2014/08/27 职场文书
教师个人事迹材料
2014/12/17 职场文书
女方离婚起诉书
2015/05/18 职场文书
辛亥革命观后感
2015/06/02 职场文书
golang中的struct操作
2021/11/11 Golang