http://tousu.315che.com/tousulist/serial/7805/0/0/0/8.htm
from selenium import webdriver
import requests
from lxml import etree
import os
if not os.path.exists('./汽车用户消费投诉数据'):
os.mkdir('./汽车用户消费投诉数据')
#driver=webdriver.Firefox()
#driver.get('http://tousu.315che.com/tousudetail/104189/') #任务一
wz='http://tousu.315che.com/tousulist/serial/7805/0/0/0/%d.htm'
ua = {'User-Agent':'Mozilla/5.0 (Mozilla/5.0 (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.53'}
for page in range(1,376):
wz1=format(wz%page)
hq = requests.get(url=wz1, headers=ua).text
jx = etree.HTML(hq)
lst=jx.xpath('//div[@class="tousu-filter-list"]/ul[@class="clearfix"]/li/a/@href')
for i in lst:
wz2=i
hq2 = requests.get(url=wz2, headers=ua)
hq2.encoding='utf-8'
hq2=hq2.text
jx = etree.HTML(hq2)
lst2=jx.xpath('//div[@class="article box1"]/div[@class="complaints-appeal-info"]/p/text()')
for n in lst2:
dz='./汽车用户消费投诉数据/数据.csv'
with open(dz,'a',encoding='utf-8')as fp:
fp.write(n+'\n')
fp.close()
具体步骤
进入爬取页面
获取详情页面url
即由http://tousu.315che.com/tousulist/serial/7805/0/0/0/8.htm
进入http://tousu.315che.com/tousudetail/117828/
wz='http://tousu.315che.com/tousulist/serial/7805/0/0/0/%d.htm'
ua = {'User-Agent':'Mozilla/5.0 (Mozilla/5.0 (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.53'}
hq = requests.get(url=wz1, headers=ua).text
jx = etree.HTML(hq)
lst=jx.xpath('//div[@class="tousu-filter-list"]/ul[@class="clearfix"]/li/a/@href')
详情页面获取投诉信息
for i in lst:
wz2=i
hq2 = requests.get(url=wz2, headers=ua)
hq2.encoding='utf-8'
hq2=hq2.text
jx = etree.HTML(hq2)
lst2=jx.xpath('//div[@class="article box1"]/div[@class="complaints-appeal-info"]/p/text()')
翻页循环
for page in range(1,376):
wz1=format(wz%page)
hq = requests.get(url=wz1, headers=ua).text