兄弟们断更了两天,去了趟天津看了看水上公园,毛了毛天津之眼。今天采集一下量子位网站的数据,这里面都是一些关于新能源汽车的介绍。
- 话不多说直接开始,还是先看一下网站结构,网站地址
映入眼帘的就是要爬的数据列表了,看起来和其他的网站结构没什么两样,那就直接抓一下翻页的包看看数据接口是什么样子的
点击加载更多抓到两个包,第一个包没有返回数据,第二个包返回了数据,但是第一个包有两个参数,cat是个什么鬼。先继续翻页看看下一个接口
又是两个包,cat不变,就这个逻辑大概就是这样了,第一个接口缓存数据或者是添加cookies信息,第二个接口返回数据。再看一下详情页数据
详情页就是由id组成的html数据,那就ok了,先...再...最后...
- 先请求列表页再解析出来所有的数据
headers = {
'Accept': '*/*',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Referer': 'https://www.qbitai.com/category/auto',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
}
params = {
'page': '2',
}
response = requests.get('https://www.qbitai.com/category/auto', params=params, headers=headers)
res = etree.HTML(response.text)
data_list = res.xpath("//div[@class='article_list']/div[@class='picture_text']/div[@class='text_box']/h4/a")
for data in data_list:
title = data.xpath("./text()")[0]
url = data.xpath("./@href")[0]
print(title,url)
ok,然后再请求详情页数据,使用gne库解析数据
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
}
response = requests.get(url, headers=headers)
extractor = GeneralNewsExtractor()
result = extractor.extract(response.text,noise_node_list=['//div[@class="s2"]'])
print(result)
input()
ok,没什么问题