一:爬虫的目标:
打开汽车之家的链接:https://www.autohome.com.cn/beijing/,出现如下页面
我们的目标是
点击找车,然后出现如下图
我们要把图中的信息抓取到
二:实现过程
我们选择 宝马5系 然后点击找车
注意宝马5系的 data-value 是 65
如下图
因为这个网页需要做翻页,我们就点击翻页。然后抓取到了一个url链接的请求方式。
可以判断出来的是 PageIndex 是页面,表示第几页。SeriesId是车系
直接上代码
#-*-coding:utf-8-*-
from common.contest import *
'''
遇到不懂的问题?Python学习交流群:821460695满足你的需求,资料都已经上传群文件,可以自行下载!
'''
def spider():
url = 'https://dealer.autohome.com.cn/frame/Car/CarDealerList'
headers = {
"Host":"dealer.autohome.com.cn",
"Connection":"keep-alive",
"Accept":"*/*",
"X-Requested-With":"XMLHttpRequest",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36",
"Referer":"https://dealer.autohome.com.cn/frame/car/GetDealerByCar?SeriesId=65&ProvinceId=110000&CityId=110100",
"Accept-Encoding":"gzip, deflate, br",
"Accept-Language":"zh-CN,zh;q=0.9",
"Cookie":"__ah_uuid=B5EFCA0A-C638-43C8-8D4F-1CEB07A1E9BD; fvlid=15275822890176s3cfWZKlL; sessionip=119.61.28.90; area=110199;
ahpau=1; sessionid=FCACBCDA-A005-4F82-8E88-C27091B3A127%7C%7C2018-05-29+16%3A24%3A54.092%7C%7Cwww.baidu.com;
sessionvid=6B39A12E-5580-4522-9800-316DA038715A; sessionuid=FCACBCDA-A005-4F82-8E88-C27091B3A127%7C%7C2018-05-29+16%3A24%3A54.092%7C%7Cwww.baidu.com;
papopclub=0AFEB345FB77A406EFE118CB317CE733; pepopclub=7C16F37F6D8E038ABDFD155664996DA2;
historybbsName4=c-3170%7C%E5%A5%A5%E8%BF%AAA3; pvidlist=7ab2cb54-f4a0-420c-be56-dff130b41d846:149875:216460:0:1:1036776;
mpvareaid=2018278; mallsfvi=15275826304503kGG3Qam%7Cwww.autohome.com.cn%7C2018278; mallslvi=2018278%7Cwww.autohome.com.cn%7C15275826304503kGG3Qam;
Hm_lvt_9924a05a5a75caf05dbbfb51af638b07=1527582690; ahsids=65_2951_3170_4851_59; ahpvno=21;
pvidchain=101061,101061,101061,2018278,3311273,3311273,3311273,3311273,3311273,3311273;
ref=www.baidu.com%7C0%7C0%7C0%7C2018-05-29+17%3A09%3A59.013%7C2018-05-29+16%3A24%3A54.092; Hm_lpvt_9924a05a5a75caf05dbbfb51af638b07=1527585001",
}
for page in range(1,11):
data = {
"BrandId":"0",
"FactoryId":"0",
"SeriesId":"65",
"ProvinceId":"110000",
"CityId":"110100",
"CountyId":"0",
"PageIndex":str(page),
"DealerKind":"1",
}
try:
result = session.get(url=url,headers=headers,verify=False,params=data).text
except:
result = session.get(url=url,headers=headers,verify=False,params=data).text
print result
spider()