相关库

import pymysql
import pymysql.cursors
from bs4 import BeautifulSoup
import requests
import random
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import codecs
from selenium.common.exceptions import TimeoutException

从数据库中读取车型(车型已经存放再数据库,这里读取车型的id,拼接到url上)

cars = []
conn = pymysql.connect(host='*******',charset='utf8',user=*******',passwd='*****',db='mysql',cursorclass=pymysql.cursors.DictCursor)try:cur = conn.cursor()cur.execute("USE data_etl")cur.execute("select distinct(car_id),car_name from user_car_port")item = cur.fetchone()count = 0while item is not None:cars.append(item)count+=1item = cur.fetchone()print(count)
finally:conn.close()

由于汽车之家反爬比较复杂,我们直接调用浏览器接口

driver = webdriver.Chrome('chromedriver.exe')
def getCarPriceOffSale(innerHtml):button = 0.0top = 0.0print("此车型已经停售!")bsObj = BeautifulSoup(innerHtml)try:spanPrice = bsObj.findAll("span",{"class":"price"})[0]if spanPrice is not None:strongPrice = spanPrice.find("strong",{"class":"red"})if strongPrice is not None:text = strongPrice.textif text is not None:prices = text.split("-")prices = text.split("-")prices[0] = prices[0].replace("万","")prices[0] = prices[0].replace("元","")button = float(prices[0])if(len(prices) == 2):prices[1] = prices[1].replace("万","")prices[1] = prices[1].replace("元","")top = float(prices[1])else:top = buttonelse:print("价格字段为空")else:print("价格strong为空")else:print("价格span为空")except Exception:print("程序出错!停售车型")return button,top

处理在售车型的价格 信息

def getCarPriceOnSale(innerHtml):button = 0.0top = 0.0print("此车型在售")bsObj = BeautifulSoup(innerHtml)try:ddprice = bsObj.findAll("dd")[0]if ddprice is not None:a = ddprice.find("a",{"class":"emphasis"})if a is not None:text = a.textprices = text.split("-")prices[0] = prices[0].replace("万","")prices[0] = prices[0].replace("元","")button = float(prices[0])if(len(prices) == 2):prices[1] = prices[1].replace("万","")prices[1] = prices[1].replace("元","")top = float(prices[1])else:top = buttonelse:print("此车型暂时无法查询价格")except Exception:print("程序出错!在售车型")return button,top

处理停售车型的价格信息

def getCarPrice(carId):button = 0.0top = 0.0try:driver.get(url+str(carId))wait = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CLASS_NAME,"information-summary")))ele = driver.find_element_by_class_name("information-price").get_attribute('innerHTML')button,top=getCarPriceOnSale(ele)except TimeoutException:try:wait = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CLASS_NAME,"car_price")))ele = driver.find_element_by_class_name("car_price").get_attribute('innerHTML')button,top=getCarPriceOffSale(ele)except TimeoutException:print("此车型有问题:"+str(carId))   return button,top

遍历数据库所有车型的id

for car in cars:id = car["car_id"]time.sleep(random.randint(1,5))button,top = getCarPrice(id)if button == 0.0 and top == 0.0:car["button"] = 9999car["top"] = 9999else:car["button"] = buttoncar["top"] = top

python爬虫实战 爬取汽车之家上车型价格相关推荐

  1. Python爬虫实战爬取租房网站2w+数据-链家上海区域信息(超详细)

    Python爬虫实战爬取租房网站-链家上海区域信息(过程超详细) 内容可能有点啰嗦 大佬们请见谅 后面会贴代码 带火们有需求的话就用吧 正好这几天做的实验报告就直接拿过来了,我想后面应该会有人用的到吧 ...

  2. python爬虫实战---爬取大众点评评论

    python爬虫实战-爬取大众点评评论(加密字体) 1.首先打开一个店铺找到评论 很多人学习python,不知道从何学起. 很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手. 很多 ...

  3. python爬虫实战-爬取视频网站下载视频至本地(selenium)

    #python爬虫实战-爬取视频网站下载视频至本地(selenium) import requests from lxml import etree import json from selenium ...

  4. python爬虫实战-爬取微信公众号所有历史文章 - (00) 概述

    http://efonfighting.imwork.net 欢迎关注微信公众号"一番码客"获取免费下载服务与源码,并及时接收最新文章推送. 最近几年随着人工智能和大数据的兴起,p ...

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

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

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

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

  7. python爬虫实战--爬取猫眼专业版-实时票房

    小白级别的爬虫入门 最近闲来无事,发现了猫眼专业版-实时票房,可以看到在猫眼上映电影的票房数据,便验证自己之前学的python爬虫,爬取数据,做成.svg文件. 爬虫开始之前 我们先来看看猫眼专业版- ...

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

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

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

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

最新文章

  1. Python入门100题 | 第008题
  2. 信息系统项目管理师-项目质量管理核心知识点思维脑图
  3. C/C++学习之路: 模板和异常
  4. linux安装x11鼠标主题
  5. 音视频开发(36)---麦克风阵列声音定位解决方案
  6. MySQL 成薪资跳板了?
  7. Idea代码统计工具
  8. 吴恩达深度学习1.4笔记_Neural Networks and Deep Learning_深层神经网络
  9. Android开发之TextView的滚动显示
  10. 湖北省首条短途运输航线“荆门—武汉”航线运营
  11. matlab 离散数据 拐点,MATLAB 寻找离散点拐点
  12. python怎样分析文献综述_论文的文献综述有什么方法吗?
  13. 数据分析:某地PM2.5数据分析
  14. 我见过最“骚”的代码注释!神兽版都来了
  15. 使用GPO(Profile-Guided Optimization)优化程序
  16. maven子模块之间互相引入依赖,pom文件报错
  17. shell 字符串匹配
  18. Golang 占位符的使用
  19. 14 Babylonjs基础入门 环境搭建
  20. 利用 AI 跟踪和优化视频质量

热门文章

  1. ChatGPT 如何应用于决策?Rationale 带你狂飙!
  2. 什么是dbunit以及为什么要使用它
  3. case when then end用法
  4. 项目经理怎么做,才能让领导爱听你的工作汇报?
  5. 深入理解ASP中FSO的神奇功能
  6. [九省联考2018]一双木棋chess
  7. 搭建3D可视化智慧园区-园区导航及可视化应用-助力园区向数字化转型
  8. Hibernate Envers
  9. 可穿戴式计算机技术,可穿戴式无线网络技术方案详解(一)
  10. 树莓派不讲武德,自研双核MCU Pico,STM32哭晕在厕所!