Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Scrapy] web scrape help
downloaded scrapy and went through the tutorials and still trying to understand the selector
aspect of scraping. So I thought scrape a different quotes web page:
I created a new project and spider:
# -*- coding: utf-8 -*-
import scrapy

class InspiderSpider(scrapy.Spider):
    name = 'inspider'
    allowed_domains = ['']
    start_urls = ['']

    def parse(self, response):
        for quotes in response.css(''):
            yield {
                'text': quotes.css('span.text::text').extract_first(),
                'author': quotes.css('span.quote-author-name::text').extract_first()
I can extract the authors but no luck on the quote.
{"text": null, "author": "-Dr. Suess"}, {"text": null, "author": "-Marilyn Monroe"}, {"text": null, "author": null}, {"text": null, "author": "-Stephen King"}, {"text": null, "author": "-Mark Caine"}, {"text": null, "author": "-Helen Keller"}, .....
when I examine the quote element and copy xpath I get:
any help appreciated,
span.text::text tries selecting the text of a span with class text.
Such an element doesn't exist, the text is placed directly in the div.

A css selector that would work here would be simply ::text.
This technically selects all the text nodes inside the div (including the author), but .extract_first() will give you only the thing you are after.

An alternative is using an xpath such as ./text().

A couple of non-selector-related notes:
  • Your allowed_domains is being ignored since it contains full urls instead of domains (it's optional, so your code still works)
  • You should use .get() instead of .extract_first(), that's been the recommended api for a while now
thanks, I got quotes and authors.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  scrape data 1 go to next page scrape data 2 and so on alkaline3 6 280 Mar-13-2020, 07:59 PM
Last Post: alkaline3
  Scrapy-cut: Advanced Cookiecutter Scrapy Templating scriptso 2 1,791 Feb-02-2017, 07:57 PM
Last Post: scriptso

Forum Jump:

Users browsing this thread: 1 Guest(s)