题记:

互联网上关于使用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 bs4

import 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 os

import 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 = "

"

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 os

import 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 os

from 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 os

import 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("",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(""),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 json

import os

import re

import 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的样子。以后买车就用这个参考了。

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

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

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

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

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

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

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

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

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

  5. php采集汽车之家demo,Python 爬虫实例(15) 爬取 汽车之家(汽车授权经销商)...

    有人给我吹牛逼,说汽车之家反爬很厉害,我不服气,所以就爬取了一下这个网址. 本片博客的目的是重点的分析定向爬虫的过程,希望读者能学会爬虫的分析流程. 一:爬虫的目标: 打开汽车之家的链接:https: ...

  6. Python 爬虫实例(15) 爬取 汽车之家(汽车授权经销商)

    有人给我吹牛逼,说汽车之家反爬很厉害,我不服气,所以就爬取了一下这个网址. 本片博客的目的是重点的分析定向爬虫的过程,希望读者能学会爬虫的分析流程. 一:爬虫的目标: 打开汽车之家的链接:https: ...

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

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

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

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

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

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

最新文章

  1. 千人千面,撩拨你的个性化广告
  2. 【Javascript】复选框的全选与全不选
  3. 分析堆栈溢出原因_我分析了有关堆栈溢出的所有书籍。 这是最受欢迎的。
  4. 计算机应用计算机电算化题库,2014年浙江省会计电算化客观题题库
  5. python之lambda
  6. mysql修改子分区_创建MySQL子分区方法
  7. Object类的用法(一)
  8. java 修饰符 访问控制符_《Java基础知识》Java访问修饰符(访问控制符)
  9. 水晶报表技术(3)——.NET 2005 中动态水晶报表的实现
  10. python对于设计师有什么用-Python前程无忧深圳UI设计师岗位分析
  11. pkm是什么意思_2017年的你应该拥有什么样的PKM系统?
  12. 一个LaTeX论文模板
  13. php计算器源码,php 简单计算器
  14. LabVIEW的编程-程序框图入门笔记
  15. 计算机界十位顶级人物!
  16. 2021年山东省安全员C证考试题库及山东省安全员C证考试报名
  17. Graph Neural Network-Based Anomaly Detection in Multivariate Time Series 代码配置及解析
  18. 农行网上银行交电费痛苦经历
  19. android 设置iptv vlan tag的命令,IPTV+VLAN设置教程
  20. java-jar基础应用

热门文章

  1. CrossApp首款商业级应用《动漫之家》服务器源码及架设文档下载
  2. 大疆 行者无疆(一)
  3. 【登录】刷脸登录(java语言-百度云ai)
  4. python动图转换成字符画动图_基于Python实现图画转换字符画
  5. SpringBoot系列之canal和kafka实现异步实时更新
  6. RNN-BPTT 笔记
  7. 华为手机地震预警怎样设置
  8. solr的optimize()的作用
  9. 如何写出好的产品帮助文档?
  10. Google Maps瓦片(tile)地图文件下载(1-11层级)