import requests
import pymysql HOSTNAME = '127.0.0.1'
USERNAME = 'root'
PASSWORD = 'zyndev'
DATABASE = 'zyndev_new' brand = 'http://www.autohome.com.cn/ashx/AjaxIndexCarFind.ashx?type=1'
series = 'http://www.autohome.com.cn/ashx/AjaxIndexCarFind.ashx?type=3&value={}'
model = 'http://www.autohome.com.cn/ashx/AjaxIndexCarFind.ashx?type=5&value={}' def obtain_brand_info():
request_brand = requests.get(brand)
if request_brand.status_code == 200:
request_brand.close()
brand_json = request_brand.json()
if brand_json['returncode'] == 0: # 成功
brand_list = brand_json['result']['branditems']
conn = pymysql.connect(HOSTNAME, USERNAME, PASSWORD, DATABASE, charset="utf8")
cur = conn.cursor()
args = []
for item in brand_list: # 存入数据库 将 dict 转为 list
sub_arg = (item['id'], item['name'], item['bfirstletter'])
args.append(sub_arg)
print(args)
rowcount = cur.executemany('INSERT INTO auto_home_car_brand(brandid,name,bfirstletter) values(%s,%s,%s)', args)
conn.commit()
print(f"插入品牌:\n共{len(brand_dict)}\n成功插入{rowcount}条记录\n插入失败{len(brand_dict) - rowcount}条")
cur.close()
conn.close()
return brand_list
else:
raise Exception("请求失败") def obtain_series(brand_list):
for brand_info in brand_list:
request_series = requests.get(series.format(brand_info['id']))
if request_series.status_code == 200:
request_series.close()
series_json = request_series.json()
if series_json['returncode'] == 0: # 成功
factory_list = series_json['result']['factoryitems']
conn = pymysql.connect(HOSTNAME, USERNAME, PASSWORD, DATABASE, charset="utf8")
cur = conn.cursor()
args = []
series_count = 0
for factory_item in factory_list:
factory_id = factory_item['id']
factory_name = factory_item['name']
series_items = factory_item['seriesitems']
for series_item in series_items:
series_count = series_count + 1
sub_arg = (brand_info['id'], factory_id, factory_name, series_item['id'], series_item['name'],
series_item['seriesstate'], series_item['seriesorder'])
args.append(sub_arg) rowcount = cur.executemany('''INSERT INTO auto_home_car_series(brand_id, factory_id, `factory_name`,
`series_id`, `series_name`, `series_state`,`series_order`)
values(%s, %s, %s, %s, %s, %s, %s)''',args)
conn.commit()
print(f"插入车系:\n共{series_count}\n成功插入{rowcount}条记录\n插入失败{series_count - rowcount}条")
cur.close()
conn.close() def obtain_model():
conn = pymysql.connect(HOSTNAME, USERNAME, PASSWORD, DATABASE, charset="utf8")
cur = conn.cursor()
cur.execute("select series_id from auto_home_car_series")
series_list = cur.fetchall() for series in series_list:
request_model = requests.get(model.format(series[0]))
if request_model.status_code == 200:
model_json = request_model.json()
request_model.close()
if model_json['returncode'] == 0: # 成功
year_items = model_json['result']['yearitems']
conn = pymysql.connect(HOSTNAME, USERNAME, PASSWORD, DATABASE, charset="utf8")
cur = conn.cursor()
args = []
model_count = 0
for year_item in year_items:
for spec_item in year_item['specitems']:
model_count = model_count + 1
sub_args = (series[0], year_item['id'], year_item['name'], spec_item['id'], spec_item['name'],
spec_item['state'], spec_item['minprice'], spec_item['maxprice'])
args.append(sub_args) rowcount = cur.executemany('''INSERT INTO auto_home_car_model(
`series_id`, `year_id`, `year_name`, `model_id`,
`model_name`, `model_state`, `min_price`, `max_price`)
values(%s, %s, %s, %s, %s, %s, %s, %s)''', args)
conn.commit()
print(f"插入车型:\n共{model_count}\n成功插入{rowcount}条记录\n插入失败{model_count - rowcount}条")
cur.close()
conn.close() def main():
#brand_list = obtain_brand_info()
#obtain_series(brand_list)
obtain_model() if '__main__' == __name__:
main()

使用python抓取汽车之家车型数据的更多相关文章

  1. Python 爬取汽车之家口碑数据

    本文仅供学习交流使用,如侵立删!联系方式见文末 汽车之家口碑数据 2021.8.3 更新 增加用户信息参数.认证车辆信息等 2021.3.24 更新 更新最新数据接口 2020.12.25 更新 添加 ...

  2. [Python爬虫] 之十八:Selenium +phantomjs 利用 pyquery抓取电视之家网数据

    一.介绍 本例子用Selenium +phantomjs爬取电视之家(http://www.tvhome.com/news/)的资讯信息,输入给定关键字抓取资讯信息. 给定关键字:数字:融合:电视 抓 ...

  3. 一篇文章教会你用Python抓取抖音app热点数据

    今天给大家分享一篇简单的安卓app数据分析及抓取方法.以抖音为例,我们想要抓取抖音的热点榜数据. 要知道,这个数据是没有网页版的,只能从手机端下手. 首先我们要安装charles抓包APP数据,它是一 ...

  4. 手把手教你用Python抓取AWS的日志(CloudTrail)数据

    数据时代,利用数据做决策是大数据的核心价值. 本文手把手,教你使用python进行AWS的CloudTrail配置,进行日志抓取.进行数据分析,发现数据价值! 如今是云的时代,许多公司都把自己的IT架 ...

  5. 浅谈如何使用python抓取网页中的动态数据

    我们经常会发现网页中的许多数据并不是写死在HTML中的,而是通过js动态载入的.所以也就引出了什么是动态数据的概念, 动态数据在这里指的是网页中由Javascript动态生成的页面内容,是在页面加载到 ...

  6. python抓取网页中的动态数据

    一.概念 网页中的许多数据并不是写死在HTML中的,而是通过js动态载入的.所以也就引出了什么是动态数据的概念,动态数据在这里指的是网页中由Javascript动态生成的页面内容,是在页面加载到浏览器 ...

  7. Python抓取淘宝IP地址数据

    def fetch(ip): url = 'http://ip.taobao.com/service/getIpInfo.php?ip=' + ip result = [] try: response ...

  8. python抓取NBA现役球员基本信息数据

    链接:http://china.nba.com/playerindex/ 所需获取JSON数据页面链接:http://china.nba.com/static/data/league/playerli ...

  9. python抓取NBA现役球员基本信息数据并进行分析

    链接:http://china.nba.com/playerindex/ 所需获取JSON数据页面链接:http://china.nba.com/static/data/league/playerli ...

随机推荐

  1. reactor-core

    <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-core&l ...

  2. 吴裕雄--天生自然python Google深度学习框架:经典卷积神经网络模型

    import tensorflow as tf INPUT_NODE = 784 OUTPUT_NODE = 10 IMAGE_SIZE = 28 NUM_CHANNELS = 1 NUM_LABEL ...

  3. Qt QGraphicsScene||GraphicsView函数刷新多次内存溢出问题

    需将QGraphicsScene *scene = new QGraphicsScene;放入上面声明头文件中声明: cpp文件中声明: 使用: 需要添加这个 scene->clear(); 这 ...

  4. linux 下c 链接so 库

    gcc -shared -fPIC -o libname.so  *.c  //生成so库 gcc main.c -om  -Lpath -lname //链接测试so 库 但是生成可执行程序执行时报 ...

  5. ubuntu16.04更换 apt-get 阿里源

    deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties deb ht ...

  6. [GX/GZOI2019]与或和(单调栈+按位运算)

    首先看到与或,很显然想到按照位拆分运算.然后就变成了0/1矩阵,要使矩阵在当前位与为1,则矩阵全为1,如果是或为1,则是矩阵不全为0,然后求全为0/1的矩阵个数即可.记录c[i][j]表示以a[i][ ...

  7. iOS 自定义只有年月的DatePikerView

    头文件: @interface YearMonthPikerView : UIView @property (nonatomic,copy) void(^cancelBlock)(); @proper ...

  8. 2019-ECfinal-M题-value

    题目传送门 sol:每个下标都有选和不选两种情况,所以总方案数是$2^{n}$,在$n$最大是$100000$的情况下不符合要求.可以这样想,假设$i^{p}=k$有符合题目要求的解,还有一个整数$j ...

  9. Linux centos 下安装redis

    一.安装编译工具及库文件      yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel 二.选择安装文件 ...

  10. pip 通过pqi切换源到国内镜像

    pip install pqipqi lspqi use aliyun # pqi use tuna   清华