python抓取数据时失败_python网页爬虫之列车时刻表的抓取(5)-错误数据的处理
抓取了99%的数据还剩一点点瑕疵要处理掉.
在脚本的运行日志里不和谐的一段:
05-29 18:14 transchedule INFO train number 5611 processed
05-29 18:14 transchedule ERROR 2:2
05-29 18:14 transchedule INFO start process http://www.tielu.org/TrainList/TrainList-5.html
05-29 18:14 transchedule INFO train number 5612 processed
05-29 18:14 transchedule ERROR 7 黄:2
5611和5612的椑木镇和黄磏,这两个生僻字直接导致了我的脚本少了14条途经车站的数据....数据该升级啦
铁路网的数据: 这边是极品时刻表的:
原来他们的数据源是一样的.....到他们家论坛提个BUG去.
把5611和5612存到本地,改掉html的gb2312编码,补好数据,完工啦.
========数据补充脚本========
#! /usr/bin/env python
#coding=utf8
#import urllib2
from HTMLParser import HTMLParser
from pyquery import PyQuery as pq
import sqlite3,urllib2,logging,sys
from datetime import datetime
from decimal import Decimal
# 日志初始化
logFileName='./%s.log'%(datetime.now().strftime("%Y%m%d%H%M%S"))
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filename=logFileName,
filemode='w')
logger = logging.getLogger('transchedule')
hdlr = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)
global witchTrain # 所属当前页面列车的序列号
witchTrain = 0
# 初始化数据库
conn = sqlite3.connect('trainsInfo.sqlite')
c = conn.cursor()
def getScheduleInfo(i,e):
global witchTrain # 所属当前页面列车的序列号
td = pq(e)('td')
if td.eq(0).text() in ('No.',""):
witchTrain += 1
return
# 解析异常处理
if len(td) == 2:
logger.error("%s:%s"%(td.text().encode('gb18030'),len(td)))
return
# 停车时间
stopTime = datetime.strptime("00:00",'%H:%M')
try:
stopTime = datetime.strptime(td.eq(5).text(),'%H:%M')
except Exception:
#print 'stop time parse error:%s:%s'%(td.eq(5).text(),td.eq(0).text())
None
# 开车时间
startTime = datetime.strptime("00:00",'%H:%M')
try:
startTime = datetime.strptime(td.eq(6).text(),'%H:%M')
except Exception:
#print 'start time parse error:%s:%s'%(td.eq(6).text(),td.eq(0).text())
None
# 里程
range = int(td.eq(7).text()[:-2])
# 硬座
hardSeatPrice=0.0
if td.eq(8) and len(td.eq(8).text()) > 1:
hardSeatPrice=td.eq(8).text()[:-1]
# 硬卧中铺
hardBerthPrice=0.0
if td.eq(9) and len(td.eq(9).text()) > 1 and td.eq(9).text()[:-1] <> "-":
hardBerthPrice=td.eq(9).text()[:-1]
# 软座
softSeatPrice=0.0
if td.eq(10) and len(td.eq(10).text()) > 1:
softSeatPrice=td.eq(10).text()[:-1]
# 软卧下铺
softBerthPrice='0'
if td.eq(11) and len(td.eq(11).text()) > 1:
softBerth=td.eq(11).text()[:-1]
return [[witchTrain,
int(td.eq(0).text()),td.eq(1).text(),
td.eq(4).text(),stopTime,
startTime,range,
hardSeatPrice,hardBerthPrice,
softSeatPrice,softBerthPrice,]]
def insertTrainSchedule(trainInfo,scheduleData,cursor):
scheduleData[0]=trainInfo
try:
cursor.execute('insert into trains_schedule values(?,?,?,?,?,?,?,?,?,?,?)',scheduleData)
except Exception,e:
logger.error("%s %s"%(e,scheduleData[0]))
# 获取页面内的车次
def get561x(pageFilePath):
# 列车途经站点
trainSchedule = pq(filename=pageFilePath)('body center
div.ResultContent div.ResultContentLeft div.ResultContentLeftContent\
div.ResultTrainCodeContent table').eq(2)('tr')
trainSchedules = trainSchedule.map(getScheduleInfo)
global witchTrain
witchTrain = 0
for oneTrainSchedule in trainSchedules:
insertTrainSchedule(pageFilePath[:-5],oneTrainSchedule,c)
conn.commit()
logger.info("start get data...")
try:
for oneHtmlFile in ['5611.html','5612.html',]:
get561x(oneHtmlFile)
except Exception,e:
logger.error(e)
logger.info("finish...")
导出sqlite数据文件:4,880,384 bytes
python抓取数据时失败_python网页爬虫之列车时刻表的抓取(5)-错误数据的处理相关推荐
- python抓取数据时失败_python爬取页面元素失败
coursera上有不少经典课程值得收藏,于是想着把课程资源离线下来.就用requests和BeautifulSoup抓取课程资源页,分析资源链接,然后用curl批量下载.一些都很顺利,除了一个细节上 ...
- python抓取数据时失败_爬取数据缺失的补坑,Python数据爬取的坑坑洼洼如何铲平...
渣渣业余选手讲解,关于爬取数据缺失的补坑,一点点关于Python数据爬取的坑坑洼洼如何铲平,个人的一些心得体会,还有结合实例的数据缺失的补全,几点参考,仅供观赏,如有雷同,那肯定是我抄袭的! 在使用P ...
- python爬虫数据挖掘_Python网页爬虫文本处理科学计算机器学习数据挖掘兵器谱...
转载自"我爱自然语言处理":http://www.52nlp.cn,已获得授权.更多内容可见公众号:"牛衣古柳"(ID:Deserts-X). 周末时看到这篇不 ...
- python可抓取数据包括什么_Python对JS型数据抓取有什么特别好的方法吗,pythonjs型抓取,想写一个爬虫,但是需要抓...
Python对JS型数据抓取有什么特别好的方法吗,pythonjs型抓取,想写一个爬虫,但是需要抓 想写一个爬虫,但是需要抓去的的数据是js生成的,在源代码里看不到,要怎么才能抓到呢? 最好是用pyt ...
- python获取app信息的库_Python学习教程:另辟蹊径,appium抓取app应用数据了解一下...
作为爬虫工程师,没有价格不知道selenium的. 什么是selenium? Selenium原本是一个用于Web应用程序自动化测试工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一 ...
- 爬虫python爬取页面请求_Python网络爬虫第三弹《爬取get请求的页面数据》
一.urllib库 urllib是Python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在Python3中的为urllib.request和urllib. ...
- python+selenium+phantomJS爬取国家地表水水质自动监测实时数据发布系统——动态网页爬虫
一.关于phantomjs 1.介绍 PhantomJS是一个为自动化而生的利器,它本质上是一个基于webkit内核的无界面浏览器,并可使用JavaScript或CoffeeScript进行编程.由于 ...
- python爬去百度百科词条_python简单爬虫爬取百度百科python词条网页
目标分析: 目标:百度百科python词条相关词条网页 - 标题和简介 入口页:https://baike.baidu.com/item/Python/407313 URL格式: - 词条页面URL: ...
- python调用cmd合并大量ts文件失败_python之爬虫(爬取.ts文件并将其合并为.MP4文件——以及一些异常的注意事项)...
//20200115 最近在看"咱们裸熊--we bears"第一季和第三季都看完了,单单就第二季死活找不到,只有腾讯有资源,但是要vip--而且还是国语版--所以就瞄上了一个视频 ...
最新文章
- hdu1568斐波那契前4位
- DNS协议报文(RFC1035)
- tomcat实现session集群及tomcat+memcached共享session存储(四)
- MySQL数据库主从同步的3种一致性方案实现,及优劣比较
- 以下构成python循环结构的方法中_《Python程序设计与案例教程》课件chap5循环结构程序设计.ppt...
- Python爬虫入门六之Cookie的使用
- APP UI设计作品素材,还没灵感就撞墙吧!
- 一个简单的Webservice的demo,简单模拟服务
- Picasso源码的简单解析(一)
- 【MFC开发(1)】MFC的介绍、与Qt的区别
- pc企业微信hook接口,企业微信营销软件,企业微信群发
- [原创摄影]西藏行(一)从不同角度看布达拉
- linux centos 6.0下载,centos 6.0 下载 32/64位 最新高速下载地址 国内迅雷下载BT下载...
- lstm token分类模型代码解析(直接传入batch数据,测试pad和pack_padded、pad_packed三函数)
- Big Data 导论HadoopHDFS
- 计算机基础知识是一级吗,计算机二级和初级的区别 初级是一级吗
- Unable to preventDefault inside passive event listener due to target being treated as passive.
- Event Trigger 使用方法
- 第三方支付结算周期T1,T0,D1,D0区别
- LOAM源码解析1一scanRegistration
热门文章
- c语言编程数字字母排列组合,用简单的排列组合解决字符排列问题 (C语言代码)...
- Android webview 自动登陆新浪微博实现(原理)
- 百度押错了宝?10年内,共享自动驾驶汽车都是个伪命题!
- 【拓展】这篇文章把TCP/IP讲绝了!
- 最新计算机系统smart图形,SmartArt逆天了,排得了版拼得了图
- 动画动漫网址集锦持续更新
- java etl 简单实例_东方通ETL开发实例
- 记录自己的学习成长之路!
- matlab 滤波器设计 coe_现代雷达系统分析与设计
- 最新版本POI-5.2.0操作Excel本地导入库配置过程记录