import requests
import parsel
import csv
#伪装 请求头
headers = {
#用户基本信息
'Cookie': 'fvlid=1652681259525EL1JOKlG4jwu; sessionid=00da8547-d1be-4b57-aaff-70118f7a00cc; sessionip=60.210.137.50; area=370705; sessionvisit=e3633597-6c82-4400-931d-26860d0ca426; sessionvisitInfo=00da8547-d1be-4b57-aaff-70118f7a00cc|cn.bing.com|0; Hm_lvt_d381ec2f88158113b9b76f14c497ed48=1652681260; che_sessionid=454B1328-3097-475C-BCF3-7A5F7D5D5762%7C%7C2022-05-16+14%3A07%3A40.049%7C%7Ccn.bing.com; che_sessionvid=BD3C8084-56F8-4FE7-B6F1-E50FC81FB11D; UsedCarBrowseHistory=0%3A43533318; userarea=0; listuserarea=0; ahpvno=13; ahuuid=C077C62F-23F6-406B-89D7-27E5B5AE4DCE; Hm_lpvt_d381ec2f88158113b9b76f14c497ed48=1652682194; v_no=13; visit_info_ad=454B1328-3097-475C-BCF3-7A5F7D5D5762||BD3C8084-56F8-4FE7-B6F1-E50FC81FB11D||-1||-1||13; che_ref=cn.bing.com%7C0%7C0%7C0%7C2022-05-16+14%3A23%3A14.350%7C2022-05-16+14%3A07%3A40.049; showNum=13; sessionuid=00da8547-d1be-4b57-aaff-70118f7a00cc'
#浏览器基本信息
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.47'
}
url = 'https://www.che168.com/china/list/' #发送请求
respones = requests.get(url=url,headers=headers)
#获取数据,网页源代码
html_data = (respones.text)
#解析数据(网页构造,css样式选择器)
selector = parsel.Selector(html_data)
lis = selector.css('.viewlist_ul li')
for li in lis:
card_name = li.css('.card-name::text').get()
cards_unit = li.css('.cards-unit::text').get()
pirce = li.css('.pirce em::text').get()
if pirce == None:
pirce = li.css('.pirce em::text').get()
print(card_name,cards_unit,pirce)
#保存数据
with open('汽车之家.csv',mode='a',newline='', encoding='utf-8') as d:
csv_writer = csv.writer(d)
csv_writer.writerow([card_name,cards_unit,pirce])
【Python源码】爬取二手车之家信息
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《【Python源码】爬取二手车之家信息》
文章链接:https://www.gebizhan.com/1194.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
文章名称:《【Python源码】爬取二手车之家信息》
文章链接:https://www.gebizhan.com/1194.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。