1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] How to get text when it is after a class or span tag?

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    I have been able to get the information I want, using Scrapy, to get information on car models, types, features etc.

    However, I cannot get the price. All the other information is in stuff like a class, h4 element etc. But this is after the class and in its own selection. I have been using .css to get the info. But can not figure out how to get this text.

    There is multiple records on the page. Using xpath the next car goes up by one in the xpath tag

    /html/body/main/div/div/div\[2\]/div/div\[2\]/div\[3\]/div\[**49**\]/div/div\[2\]/div\[1\]/div/div/div/div/span/text()

    /html/body/main/div/div/div\[2\]/div/div\[2\]/div\[3\]/div\[**50**\]/div/div\[2\]/div\[1\]/div/div/div/div/span/text()


    Here is the URL :https://www.stoneytrailmazda.com/vehicles/new/?view=grid&sc=new

    [​IMG]

    Any advice on what would work would be greatly appreciated.

    I have tried to use xpath, classes, Scrapy, Scrapy-Playwright but I can not figure it out.

    Below is the code so far:

    class ScrapingClubSpider(scrapy.Spider):
    name = "stonymazda"
    allowed_domains = ["stoneytrailmazda.com"]

    def start_requests(self):
    url = "https://www.stoneytrailmazda.com/vehicles/new/"
    yield scrapy.Request(url, meta={"playwright": True})

    def parse(self, response):
    # iterate over the product elements
    for product in response.css(".vehicle-card__details"):
    # scrape product data
    url = product.css("a").attrib["href"]
    Model = product.css(".vehicle-card__title::text").get()
    Price = product.css("#ae-skip-to-content > div > div > div.pl-sm.pr-sm > div > div.col.primary-col.dealertrack-vehicle-wrapper > div.row.grid-row > div:nth-child(50) > div > div.vehicle-card__details > div.price-block > div > div > div > div > span::text").get()
    #price = product.css("h5::text").get()

    # add the data to the list of scraped items
    yield {
    "url": url,
    "Model": Model,
    #"name": name,
    "Price": Price

    Continue reading...

Compartilhe esta Página