python爬虫,58汽车信息爬取(selenium)

coding=utf-8

“”"
author:lei
function:
“”"

import time
from selenium import webdriver
import json

class Car(object):
def init(self):
self.url = “https://sanhe.58.com/ershouche/”
options = webdriver.ChromeOptions()
options.binary_location = r"D:\文件2\Google\Chrome\Application\chrome.exe"
self.webdriver = webdriver.Chrome(r"D:\文件\软件\chromedriver_win32\chromedriver.exe", options=options)

def parse_data(self):
    el_list = self.webdriver.find_elements_by_xpath("//ul[@class='infos infos-card h-clearfix']/li[@class='info']")

    # print(len(el_list))
    temp_list = []
    for el in el_list:
        temp = {}
        temp["car_name"] = el.find_element_by_xpath(".//div[1]/a/h2/span").get_attribute("title")
        temp["car_value"] = el.find_element_by_xpath(".//div[2]/b").text
        temp["information"] = el.find_element_by_xpath(".//div[1]/div[2]").text
        print(temp)
        temp_list.append(temp)

    return temp_list

def save_list(self, temp_list):
    print(temp_list)
    # with open("car.json", "a", encoding="utf-8") as f:
    #     f.write(json.dumps(temp_list, ensure_ascii=False))
    #     print("保存成功!")

def run(self):
    self.webdriver.get(self.url)

    while True:
        temp_list = self.parse_data()

        self.save_list(temp_list)
        try:
            next_page = self.webdriver.find_element_by_xpath("//a[@class='next']")
            self.webdriver.execute_script("scrollTo(0, 10000)")
            next_page.click()
        except:
            break

    self.webdriver.quit()

if name == ‘main’:
car = Car()
car.run()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值