import requests
import parsel
import openpyxl
import time
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3760.400 QQBrowser/10.5.4083.400',
'Referer': 'https://image.baidu.com/'
}
for page in range(1,10):
target = f"http://www.12365auto.com/zlts/273-0-0-0-0-0_0-0-0-0-0-0-0-{page}.shtml"
print(target)
response = requests.get(target,headers=headers).text
html = parsel.Selector(response)
# 搜索编号
num = html.xpath('//tr/td[1]/text()').getall()
# 公司名称
name = html.xpath('//tr/td[2]/text()').getall()
# 车辆品牌
car = html.xpath('//tr/td[3]/text()').getall()
# 车辆款式
cars = html.xpath('//tr/td[4]/text()').getall()
# 问题信息
carinfo = html.xpath('//tr/td/a/text()').getall()
# 投诉时间
timer = html.xpath('//tr/td[7]/text()').getall()
# 投诉状态
infos = html.xpath('//tr/td/em/text()').getall()
datalist = []
for i in range(len(num)):
#创建一个新列表接受
data = []
data.append(num[i])
data.append(name[i])
data.append(car[i])
data.append(cars[i])
data.append(carinfo[i])
data.append(timer[i])
data.append(infos[i])
#再次添加进一个列表
datalist.append(data)
wb = openpyxl.Workbook()
wb.guess_types = True
ws = wb.active
ws.append(['投诉编号','投诉品牌','投诉车系','投诉车型','问题简述','投诉时间','投诉状态'])
for each in datalist:
print(each)
ws.append(each)
time.sleep(0.5)
wb.save("车辆问题.xlsx")
|