Can't grab the title of different listings from a webpage using scrapy

Viewed 286

I'm trying to parse the title of different listings from this webpage. The titles are not dynamic as they are available in page source. However, it is necessary to send cookies in the first place to grab the titles. I've tried with the following way to scrape the titles of the listings but it doesn't seem to work.

My attempt so far:

import scrapy
from scrapy.crawler import CrawlerProcess

class ControllerSpider(scrapy.Spider):
    name = 'controller'
    start_urls = [
        'https://www.controller.com/listings/aircraft/for-sale/list?SortOrder=23&scf=False&page=1'
    ]

    def start_requests(self):
        for i,url in enumerate(self.start_urls):
            yield scrapy.Request(url,meta={'cookiejar': i},callback=self.parse)
    
    def parse(self,response):
        for item in response.css(".listing-name > a[href]::text").getall():
            yield {"title":item}

if __name__ == "__main__":
    c = CrawlerProcess({
        'USER_AGENT': 'Mozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
    })
    c.crawl(ControllerSpider)
    c.start()

How can I grab the titles of different listings from that webpage making use of cookies?

PS I do not wish to hardcode the cookies.

1 Answers

If you use a scraping browser extension you don't have to manually deal with the cookies. Visit the site normally thus getting the cookies and afterwards scrape it.

https://github.com/get-set-fetch/extension is an open source extension that can handle your scenario just by specifying CSS selectors for link navigation and content extraction.

I've played a bit with the site and created a scraping configuration for you containing the required CSS selectors for navigation (next page, aircraft detail page) and scraping (year, model, manufacturer, price)

"eLtI4gnapZQ9b8MgEIb/CuoQtQO4SZolktWta4Z2zELwYSNhjI5z3P77AmrS2ulH0gwI7OM9ne7eh+PVVH9qPKBQXXsCBRmywKSrWMxopGWub3eAR1YaIh/WRTEMg1CjVIU1gYyrQyENKpSaCt0hD9JCDj0+d0gbrADLxXIWlC6fkhdmPjannI8ZrIF4iEsnYPghEA8oPfBoUTKKf3r4Gl7v/wPoVPcDAlPXj5BQjXQ1vHw1djblxgNmzsP0+Tjb62JHjsfmt+kgHLxSRD5vqdlbV5m9+JgWT7mZjPHD1PKdES2rKSzflnUuHKLl1Hk+XwmlmcBuWDtquGqMrW4Xd0wED4rvpe3TO/UGErfuN8lyKmml63Wsp0f4Q/pwIu3ihKLGo1Fw/Ju/bi4g9TIy3wGjz0AS"

Inside the extension do: new project > config hash > paste the above hash (without the quotes) > save, scrape, view results > export as csv.

Each csv row will have year, manufacturer, model, price. I've set some limits so only the first 4 result pages will be scraped but you can disable it by setting the corresponding value to -1.

Disclaimer: I'm the extension author.

Related