题记:

  互联网上关于使用python3去爬取汽车之家的汽车数据(主要是汽车基本参数,配置参数,颜色参数,内饰参数)的教程已经非常多了,但大体的方案分两种:

  1.解析出汽车之家某个车型的网页,然后正则表达式匹配出混淆后的数据对象与混淆后的js,并对混淆后的js使用pyv8进行解析返回正常字符,然后通过字符与数据对象进行匹配,具体方法见这位园友,传送门:https://www.cnblogs.com/my8100/p/js_qichezhijia.html (感谢这位大神前半部分的思路)

2.解析出汽车之家某个车型的网页,然后正则表达式匹配出混淆后的数据对象与混淆后的js,针对混淆后的js进行进行手动匹配,因为混淆的js大概分为8大类(无参数 返回常量,无参数 返回函数,参数等于返回值函数,无参数 返回常量,无参数 返回常量中间无混淆代码,字符串拼接时使无参常量,字符串拼接时使用返回参数的函数),然后通过正则表达式进行解析出8类内容并进行逐个替换,最终也会返回一个带有顺序的字符串,将这个字符串与前边的数据对象再次替换,最终数据对象中的所有span都会被替换成中文,具体操作见园友的地址,传送门:https://www.cnblogs.com/dyfblog/p/6753251.html (感谢这位大神前半部分的思路)

不过鉴于作者技术有限,上述的两种方案,我都没有完整的执行完成,哪怕花了一周的时间也没有,但是没有办法,谁让我是一个很爱钻牛角尖的人呢,下一步提出我自己琢磨出来的方案,流程上稍微有点复杂,但是稳打稳扎,还是可以爬出来的,好了话不多说了,贴出步骤;

1.获取所有车型的网页,保存到本地:

import bs4import requests as req'''第一步,下载出所有车型的网页。'''def mainMethod():    '''    解析汽车之家所有车型数据保存到D盘    '''    li = [chr(i) for i in range(ord("T"),ord("Z")+1)]    firstSite="https://www.autohome.com.cn/grade/carhtml/"    firstSiteSurfixe=".html"    secondSite = "https://car.autohome.com.cn/config/series/"    secondSiteSurfixe = ".html"

    for a in li:        if a is not None:            requestUrl = firstSite+a+firstSiteSurfixe            print(requestUrl)            #开始获取每个品牌的车型            resp = req.get(requestUrl)            # print(str(resp.content,"gbk"))            bs = bs4.BeautifulSoup(str(resp.content,"gbk"),"html.parser")            bss = bs.find_all("li")            con = 0            for b in bss:                d = b.h4                if d is not None:                    her = str(d.a.attrs['href'])                    her = her.split("#")[0]                    her = her[her.index(".cn")+3:].replace("/",'')                    if her is not None:                        secSite = secondSite +her + secondSiteSurfixe                        print("secSite="+secSite)                        # print(secSite)                        #奥迪A3                        if her is not None:                            resp = req.get(secSite)                            text = str(resp.content,encoding="utf-8")                            print(a)                            fil = open("d:\\autoHome\\html\\"+str(her),"a",encoding="utf-8")                            fil.write(text)                    con = (con+1)            else:                print(con)if __name__ =="__main__":    mainMethod()

2.解析出每个车型的关键js并拼装成一个html,保存到本地。

import osimport re'''第二步,解析出每个车型的关键js拼装成一个html'''if __name__=="__main__":    print("Start...")    rootPath = "D:\\autoHome\\html\\"    files = os.listdir(rootPath)    for file in files:        print("fileName=="+file.title())        text = ""        for fi in open(rootPath+file,'r',encoding="utf-8"):            text = text+fi        else:            print("fileName=="+file.title())        #解析数据的json        alljs = ("var rules = '2';"                 "var document = {};"                 "function getRules(){return rules}"                 "document.createElement = function() {"                 "      return {"                 "              sheet: {"                 "                      insertRule: function(rule, i) {"                 "                              if (rules.length == 0) {"                 "                                      rules = rule;"                 "                              } else {"                 "                                      rules = rules + '#' + rule;"                 "                              }"                 "                      }"                 "              }"                 "      }"                 "};"                 "document.querySelectorAll = function() {"                 "      return {};"                 "};"                 "document.head = {};"                 "document.head.appendChild = function() {};"

                 "var window = {};"                 "window.decodeURIComponent = decodeURIComponent;")        try:            js = re.findall('(\(function\([a-zA-Z]{2}.*?_\).*?\(document\);)', text)            for item in js:                alljs = alljs + item        except Exception as e:            print('makejs function exception')

        newHtml = "<html><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><head></head><body>    <script type='text/javascript'>"        alljs = newHtml + alljs+" document.write(rules)</script></body></html>"        f = open("D:\\autoHome\\newhtml\\"+file+".html","a",encoding="utf-8")        f.write(alljs)        f.close()

3.解析出每个车型的数据json,比如var config  ,var option , var bag  var innerbag..但我就解析了基本信息跟配置信息,其他的无所谓。

import osimport re'''第三步 解析出每个车型的数据json,保存到本地。'''if __name__=="__main__":    print("Start...")    rootPath = "D:\\autoHome\\html\\"    files = os.listdir(rootPath)    for file in files:        print("fileName=="+file.title())        text = ""        for fi in open(rootPath+file,'r',encoding="utf-8"):            text = text+fi        else:            print("fileName=="+file.title())        #解析数据的json        jsonData = ""        config = re.search('var config = (.*?){1,};',text)        if config!= None:            print(config.group(0))            jsonData = jsonData+ config.group(0)        option = re.search('var option = (.*?)};',text)        if option != None:            print(option.group(0))            jsonData = jsonData+ option.group(0)        bag = re.search('var bag = (.*?);',text)        if bag != None:            print(bag.group(0))            jsonData = jsonData+ bag.group(0)        # print(jsonData)        f = open("D:\\autoHome\\json\\"+file,"a",encoding="utf-8")        f.write(jsonData)        f.close()

4.生成样式文件,保存 到本地。

import osfrom selenium import webdriver

'''    第四步,浏览器执行第二步生成的html文件,抓取执行结果,保存到本地。'''class Crack():    def __init__(self,keyword,username,passod):        self.url = 'https://www.baidu.com'        self.browser = webdriver.Chrome('E:\work\ChromePortable\App\Google Chrome\chromedriver.exe')

if __name__=="__main__":    lists = os.listdir("D:/autoHome/newHtml/")    crack = Crack('测试公司','17610177519','17610177519')    for fil in lists:        file = os.path.exists("D:\\autoHome\\content\\"+fil)        if file :            print('文件已经解析。。。'+str(file))            continue        print(fil)        crack.browser.get("file:///D:/autoHome/newHtml/"+fil+"")        text = crack.browser.find_element_by_tag_name('body')        print(text.text)        f = open("D:\\autoHome\\content\\"+fil,"a",encoding="utf-8")        f.write(text.text)    else:        f.close()        crack.browser.close()

5.读取样式文件,匹配数据文件,生成正常数据文件

 import osimport re'''第五步 匹配样式文件与json数据文件,生成正常的数据文件。'''if __name__ =="__main__":    rootPath = "D:\\autoHome\\json\\"    listdir = os.listdir(rootPath)    for json_s in listdir:        print(json_s.title())        jso = ""        #读取json数据文件        for fi in open(rootPath+json_s,'r',encoding="utf-8"):            jso = jso+fi        content = ""        #读取样式文件        spansPath = "D:\\autoHome\\content\\"+json_s.title()+".html"        # print(spansPath)        for spans in  open(spansPath,"r",encoding="utf-8"):            content = content+ spans        print(content)        #获取所有span对象        jsos = re.findall("<span(.*?)></span>",jso)        num = 0        for js in jsos:            print("匹配到的span=>>"+js)            num = num +1            #获取class属性值            sea = re.search("'(.*?)'",js)            print("匹配到的class==>"+sea.group(1))            spanContent = str(sea.group(1))+"::before { content:(.*?)}"            #匹配样式值            spanContentRe = re.search(spanContent,content)            if spanContentRe != None:                if sea.group(1) != None:                    print("匹配到的样式值="+spanContentRe.group(1))                    jso = jso.replace(str("<span class='"+sea.group(1)+"'></span>"),re.search("\"(.*?)\"",spanContentRe.group(1)).group(1))        print(jso)        fi = open("D:\\autoHome\\newJson\\"+json_s.title(),"a",encoding="utf-8")        fi.write(jso)        fi.close()

6.到前五步已经可以看到json数据文件都已经是混淆前的了,说明已经爬取成功了。

7.读取数据文件,生成excel

import jsonimport osimport reimport xlwt'''第七步读取数据文件,生成excel'''if __name__ == "__main__":    rootPath = "D:\\autoHome\\newJson\\"    workbook = xlwt.Workbook(encoding = 'ascii')#创建一个文件    worksheet = workbook.add_sheet('汽车之家')#创建一个表    files = os.listdir(rootPath)    startRow = 0    isFlag = True #默认记录表头    for file in files:        list = []        carItem = {}        print("fileName=="+file.title())        text = ""        for fi in open(rootPath+file,'r',encoding="utf-8"):            text = text+fi        # else:            # print("文件内容=="+text)        #解析基本参数配置参数,颜色三种参数,其他参数        config = "var config = (.*?);"        option = "var option = (.*?);var"        bag = "var bag = (.*?);"

        configRe = re.findall(config,text)        optionRe = re.findall(option,text)        bagRe = re.findall(bag,text)        for a in configRe:            config = a        print("++++++++++++++++++++++\n")        for b in optionRe:            option = b            print("---------------------\n")        for c in bagRe:            bag = c        # print(config)        # print(option)        # print(bag)

        # print(bag)        try:            config = json.loads(config)            option = json.loads(option)            bag = json.loads(bag)            # print(config)            # print(option)            # print(bag)            path = "D:\\autoHome\\autoHome.xls"

            configItem = config['result']['paramtypeitems'][0]['paramitems']            optionItem = option['result']['configtypeitems'][0]['configitems']        except Exception as e:            f =  open("D:\\autoHome\\异常数据\\exception.txt","a",encoding="utf-8")            f.write(file.title()+"\n")            continue

        #解析基本参数        for car in configItem:            carItem[car['name']]=[]            for ca in car['valueitems']:                carItem[car['name']].append(ca['value'])        # print(carItem)        #解析配置参数        for car in optionItem:            carItem[car['name']]=[]            for ca in car['valueitems']:                carItem[car['name']].append(ca['value'])

        if isFlag:            co1s = 0

            for co in carItem:                co1s = co1s +1                worksheet.write(startRow,co1s,co)            else:                startRow = startRow+1                isFlag = False

        #计算起止行号        endRowNum = startRow + len(carItem['车型名称']) #车辆款式记录数        for row in range(startRow,endRowNum):            print(row)            colNum = 0            for col in carItem:

                colNum = colNum +1                print(str(carItem[col][row-startRow]),end='|')                worksheet.write(row,colNum,str(carItem[col][row-startRow]))

        else:            startRow  = endRowNum    workbook.save('d:\\autoHome\\Mybook.xls')

8.最后打开excel文件,给你们看看。

数据量大概有8300的样子。以后买车就用这个参考了。

转载于:https://www.cnblogs.com/kangz/p/10011348.html

python3 爬取汽车之家所有车型数据操作步骤(更新版)相关推荐

  1. python3实例车代码_python3 爬取汽车之家所有车型数据操作步骤(更新版)

    题记: 互联网上关于使用python3去爬取汽车之家的汽车数据(主要是汽车基本参数,配置参数,颜色参数,内饰参数)的教程已经非常多了,但大体的方案分两种: 1.解析出汽车之家某个车型的网页,然后正则表 ...

  2. python3 爬取汽车之家所有车型操作步骤

    2019独角兽企业重金招聘Python工程师标准>>> 题记: 互联网上关于使用python3去爬取汽车之家的汽车数据(主要是汽车基本参数,配置参数,颜色参数,内饰参数)的教程已经非 ...

  3. Python爬取汽车之家所有车型数据,以后买车就用这个参考了

    欢迎点击右上角关注小编,除了分享技术文章之外还有很多福利,私信学习资料可以领取包括不限于Python实战演练.大航海计划.BAT内推.学习资料等. 前言 2018年马上就要过去了,经过一年的奋斗,我相 ...

  4. python3 selenium webdriver.Chrome php 爬取汽车之家所有车型详情数据[开源版]

    介绍 本接口是车型库api的补充,用于爬取汽车之家所有车型详情数据 开源地址:https://gitee.com/web/CarApi/tree/master/python 软件架构 python3 ...

  5. python爬虫实战 爬取汽车之家上车型价格

    相关库 import pymysql import pymysql.cursors from bs4 import BeautifulSoup import requests import rando ...

  6. nodejs爬虫——汽车之家所有车型数据

    应用介绍 项目Github地址:https://github.com/iNuanfeng/node-spider/ nodejs爬虫,爬取汽车之家所有车型数据 http://www.autohome. ...

  7. python3爬虫系列16之多线程爬取汽车之家批量下载图片

    python3爬虫系列16之多线程爬取汽车之家批量下载图片 1.前言 上一篇呢,python3爬虫系列14之爬虫增速多线程,线程池,队列的用法(通俗易懂),主要介绍了线程,多线程,和两个线程池的使用. ...

  8. WebMagic爬虫入门教程(三)爬取汽车之家的实例-品牌车系车型结构等

    本文使用WebMagic爬取汽车之家的品牌车系车型结构价格能源产地国别等:java代码 备注,只是根据url变化爬取的,没有使用爬取script页面具体的数据,也有反爬机制,知识简单爬取html标签 ...

  9. Python 爬虫实战入门——爬取汽车之家网站促销优惠与经销商信息

    在4S店实习,市场部经理让我写一个小程序自动爬取汽车之家网站上自家品牌的促销文章,因为区域经理需要各店上报在网站上每一家经销商文章的露出频率,于是就自己尝试写一个爬虫,正好当入门了. 一.自动爬取并输 ...

最新文章

  1. pycharm Enable Live Templates的作用
  2. JVM笔记详解之垃圾回收器
  3. Problem E:结构体---点坐标结构体
  4. 转: c#.net利用RNGCryptoServiceProvider产生任意范围强随机数的办法
  5. java归并排序代码_Java归并排序算法
  6. C语言格式化输出日期时间
  7. 强烈推荐收藏!3W 字Python 操作 Excel 报表自动化指南
  8. hdu5385 wyh2000 and pupil 二分图
  9. 计算机专业提升计划范文,民航个人提升计划范文个人职业规划方案.doc
  10. 2018蓝桥杯 航班时间和乘积尾零
  11. 当技术重构遇上DDD,如何实现业务、技术双赢?
  12. word里双横线怎么打_如何在word中画线 怎么在word中输入横线[图文]
  13. 滴滴云A100 40G 性能测试 V100陪练!
  14. c语言输入学号查找成绩,C语言程序设计按学号查询成绩
  15. ARM裸机开发篇3:ARM汇编语言程序设计
  16. 【免杀前置课——Windows编程】五、窗口控件——什么是控件、Windolws 窗口两大类、Windows标准控件/通用控件、控件响应的接收、创建窗口制作不同控件
  17. 计算机夏令营英语怎么说,“夏令营”英语怎么说
  18. MATLAB运用——计算三维物体的质心(水花号)
  19. 假设将15个MSA连入超级高铁网络,如何用最少的轨道连接所有MSA?
  20. 计算机网络技术职业生涯规划ppt模板,职业生涯规划ppt模板

热门文章

  1. 越过HG255D……
  2. linux烟雾传感器程序,树莓派4B之烟雾传感器模块(python3)
  3. 好书推荐《C++17 in Detail》
  4. 学习笔记 | 多态案例2-制作饮品
  5. 羽毛球拍选购常见问题解疑
  6. 分享qq报错Only fullscreen opaque activities can request orientation
  7. 仿微信图片缩放效果以及listview反弹效果
  8. 【Android O】 Service AAA does not have a SELinux domain defined
  9. 结合你的WordPress网站实际情况选择恰当的链接提交方式
  10. B-样条曲线教程(B-spline Curves Notes)目录