Python—实训day6—爬取汽车消费投诉案例
生活随笔
收集整理的這篇文章主要介紹了
Python—实训day6—爬取汽车消费投诉案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
任務:爬取某公開網站上的汽車用戶消費投訴數據('http://tousu.315che.com/tousulist/serial/55467/'),字段包含品牌、投訴內容、品牌車型、單號、投訴問題、投訴時間和經銷商,并為完成后續項目做準備。
# 導入相應庫 import requests from lxml import etreeurl = 'http://tousu.315che.com/tousulist/serial/55467/' # 第一頁的網頁鏈接# 發送請求 req = requests.get(url)req.encoding = 'UTF-8'# 解析網頁 html = etree.HTML(req.text)# 獲取數據 brand = html.xpath('//*[@id="letterTabList"]/div/a/text()') # 品牌 href = html.xpath('//*[@id="letterTabList"]/div/a/@href') # 品牌鏈接 href[0] ='http://tousu.315che.com/tousulist/serial/55467/' brand_complain = []for h in range(0, 2):req = requests.get(href[h])html = etree.HTML(req.text)brand_complain.append(html.xpath('/html/body/div[1]/div[2]/div[2]/div[1]/div[2]/div[2]/ul/li/a/@href')) #第一個品牌投訴鏈接#print(brand_complain)content = [] model = [] number = [] problem = [] time = [] store = []for i in brand_complain:for j in i:req = requests.get(j)req.encoding = 'UTF-8'html = etree.HTML(req.text)content.append(html.xpath('/html/body/div[1]/div[2]/div[2]/div[1]/div[1]/div/div[1]/p/text()')[0]) # 投訴內容model.append(html.xpath('/html/body/div[1]/div[2]/div[2]/div[1]/div[1]/div/div[3]/p[1]/text()')[0][5:]) # 車牌型號number.append(html.xpath('/html/body/div[1]/div[2]/div[2]/div[1]/div[1]/div/div[3]/p[2]/text()')[0][3:]) # 單號problem.append(html.xpath('/html/body/div[1]/div[2]/div[2]/div[1]/div[1]/div/div[3]/p[3]/text()')[0][5:]) # 問題time.append(html.xpath('/html/body/div[1]/div[2]/div[2]/div[1]/div[1]/div/div[3]/p[4]/text()')[0][5:]) # 時間store.append(html.xpath('/html/body/div[1]/div[2]/div[2]/div[1]/div[1]/div/div[3]/p[5]/text()')[0][4:]) # 商家for i in range(len(problem)):print("車牌型號:"+model[i]+"\n"+"單號:"+number[i]+"\n"+"問題:"+problem[i]+"\n"+"時間:"+time[i]+"\n"+"經銷商:"+store[i]+"\n") 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Python—实训day6—爬取汽车消费投诉案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 朱江洪功成身退 朱董配解体谁主格力(图)
- 下一篇: 钱与命的八句真实话(图)