Python—实训day6—爬取汽车消费投诉案例
生活随笔
收集整理的這篇文章主要介紹了
Python—实训day6—爬取汽车消费投诉案例
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
任務(wù):爬取某公開網(wǎng)站上的汽車用戶消費(fèi)投訴數(shù)據(jù)('http://tousu.315che.com/tousulist/serial/55467/'),字段包含品牌、投訴內(nèi)容、品牌車型、單號(hào)、投訴問(wèn)題、投訴時(shí)間和經(jīng)銷商,并為完成后續(xù)項(xiàng)目做準(zhǔn)備。
# 導(dǎo)入相應(yīng)庫(kù) import requests from lxml import etreeurl = 'http://tousu.315che.com/tousulist/serial/55467/' # 第一頁(yè)的網(wǎng)頁(yè)鏈接# 發(fā)送請(qǐng)求 req = requests.get(url)req.encoding = 'UTF-8'# 解析網(wǎng)頁(yè) html = etree.HTML(req.text)# 獲取數(shù)據(jù) 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')) #第一個(gè)品牌投訴鏈接#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]) # 投訴內(nèi)容model.append(html.xpath('/html/body/div[1]/div[2]/div[2]/div[1]/div[1]/div/div[3]/p[1]/text()')[0][5:]) # 車牌型號(hào)number.append(html.xpath('/html/body/div[1]/div[2]/div[2]/div[1]/div[1]/div/div[3]/p[2]/text()')[0][3:]) # 單號(hào)problem.append(html.xpath('/html/body/div[1]/div[2]/div[2]/div[1]/div[1]/div/div[3]/p[3]/text()')[0][5:]) # 問(wèn)題time.append(html.xpath('/html/body/div[1]/div[2]/div[2]/div[1]/div[1]/div/div[3]/p[4]/text()')[0][5:]) # 時(shí)間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("車牌型號(hào):"+model[i]+"\n"+"單號(hào):"+number[i]+"\n"+"問(wèn)題:"+problem[i]+"\n"+"時(shí)間:"+time[i]+"\n"+"經(jīng)銷商:"+store[i]+"\n") 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Python—实训day6—爬取汽车消费投诉案例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 朱江洪功成身退 朱董配解体谁主格力(图)
- 下一篇: Python—实训day7上—Nmupy