python抓取某汽车网数据解析html存入excel示例

yipeiwu_com6年前Python爬虫

1、某汽车网站地址

2、使用firefox查看后发现,此网站的信息未使用json数据,而是简单那的html页面而已

3、使用pyquery库中的PyQuery进行html的解析

页面样式:

复制代码 代码如下:

def get_dealer_info(self):
        """获取经销商信息"""
        css_select = 'html body div.box div.news_wrapper div.main div.news_list div.service_main div table tr '
        #使用火狐浏览器中的自动复制css路径得到需要位置数据
        page = urllib2.urlopen(self.entry_url).read()
        #读取页面
        page = page.replace('<br />','&')
        page = page.replace('<br/>','&')
        #由于页面中的电话信息中使用了br换行,所以在抓取的时候会产生问题
        #问题是:如果取得一对标签中的数据,中包含<br/>,会出现值得到br之前的数据,而后的数据将得不到,原因个人认为是解析html是会任务/>结尾标准       
        d = pq(page)
        #使用PyQuery解析页面,此处pq=PyQuery,因为from pyquery import PyQuery as pq
        dealer_list = []
        #创建列表用于提交到存储方法
        for dealer_div in d(css_select):
            #此处定位tr,具体数据在此标签中的td标签内
            p = dealer_div.findall('td')
            #此处p就是一个tr标签内,全部td数据的集合
            dealer = {}
            #此处的字典用于存储一个店铺的信息用于提交到列表中
            if len(p)==1:
                #此处多哥if判断是用于对数据进行处理,因为一些格式不符合最终数据的要求,需要剔除,这个快的代码按需求而定
                print '@'
            elif len(p)==6 :
                strp = p[0].text.strip()
                dealer[Constant.CITY] = p[1].text.strip()
                strc = p[2].text.strip()

                dealer[Constant.PROVINCE] = p[0].text.strip()
                dealer[Constant.CITY] = p[1].text.strip()
                dealer[Constant.NAME] = p[2].text.strip()
                dealer[Constant.ADDRESSTYPE] = p[3].text.strip()
                dealer[Constant.ADDRESS] = p[4].text.strip()
                dealer[Constant.TELPHONE] = p[5].text.strip()
                dealer_list.append(dealer) 
            elif len(p)==5:
                if p[0].text.strip() != u'省份':
                    dealer[Constant.PROVINCE] = strp
                    dealer[Constant.CITY] = p[0].text.strip()
                    dealer[Constant.NAME] = p[1].text.strip()
                    dealer[Constant.ADDRESSTYPE] = p[2].text.strip()
                    dealer[Constant.ADDRESS] = p[3].text.strip()
                    dealer[Constant.TELPHONE] = p[4].text.strip()
                    dealer_list.append(dealer)
            elif len(p)==3:
                print '@@'
        print '@@@'
        self.saver.add(dealer_list)
        self.saver.commit()

4、最终代码执行成功,得到了相应数据并存入excel中

相关文章

Python制作简单的网页爬虫

1.准备工作: 工欲善其事必先利其器,因此我们有必要在进行Coding前先配置一个适合我们自己的开发环境,我搭建的开发环境是: 操作系统:Ubuntu 14.04 LTS Pytho...

python爬虫框架talonspider简单介绍

1.为什么写这个? 一些简单的页面,无需用比较大的框架来进行爬取,自己纯手写又比较麻烦 因此针对这个需求写了talonspider: •1.针对单页面的item提取 - 具...

windows下搭建python scrapy爬虫框架步骤

windows下搭建python scrapy爬虫框架步骤

网络上现有的windows下搭建scrapy教程都比较旧,一般都是咔咔咔安装一堆软件,太麻烦,这是因为scrapy框架用到好多不同的模块,其实查阅最新的官网scrapy文档,在windo...

python抓取网页中链接的静态图片

本文实例为大家分享了python抓取网页中链接的静态图片的具体代码,供大家参考,具体内容如下 # -*- coding:utf-8 -*- #http://tieba.baid...

python爬虫入门教程--正则表达式完全指南(五)

python爬虫入门教程--正则表达式完全指南(五)

前言 正则表达式处理文本有如疾风扫秋叶,绝大部分编程语言都内置支持正则表达式,它应用在诸如表单验证、文本提取、替换等场景。爬虫系统更是离不开正则表达式,用好正则表达式往往能收到事半功倍的...