Issue
I am new to scrapy and Ubuntu 20 but have decent knowledge of functional and oops programing in Python.
I just tried the very first basic tutorial spider from [Scrapy docs] (https://docs.scrapy.org/en/latest/intro/tutorial.html) titled Extracting data in our spider.
I tried to run it but it did not work.
I wish to get the quote and author printed on terminal!!!
Thanks in Advance :)))))
This is the code
import scrapy
class FmSpider(scrapy.Spider):
name = 'fm'
def begin(self):
allowed_domains = ['http://quotes.toscrape.com/']
start_urls = ['http://quotes.toscrape.com/']
for url in start_urls:
yield scrapy.Request(url= url, callback= self.parse)
def parse(self, response):
for quote in response.css("div.quote"):
yield {
"quote": quote.css("span.text::text").get,
"author": quote.css("small.author::text").get
}
This is the terminal output
(venv) atul@vivobook:/media/atul/New Volume/Ubuntu/Code Projects/Web scraping/scrapy-tut$ scrapy runspider fm.py
2022-01-13 18:43:29 [scrapy.utils.log] INFO: Scrapy 2.5.1 started (bot: scrapybot)
2022-01-13 18:43:29 [scrapy.utils.log] INFO: Versions: lxml 4.7.1.0, libxml2 2.9.12, cssselect 1.1.0, parsel 1.6.0, w3lib 1.22.0, Twisted 21.7.0, Python 3.10.1 (main, Jan 7 2022, 19:42:47) [GCC 9.3.0], pyOpenSSL 21.0.0 (OpenSSL 1.1.1m 14 Dec 2021), cryptography 36.0.1, Platform Linux-5.11.0-46-generic-x86_64-with-glibc2.31
2022-01-13 18:43:29 [scrapy.utils.log] DEBUG: Using reactor: twisted.internet.epollreactor.EPollReactor
2022-01-13 18:43:30 [scrapy.crawler] INFO: Overridden settings:
{'SPIDER_LOADER_WARN_ONLY': True}
2022-01-13 18:43:30 [scrapy.extensions.telnet] INFO: Telnet Password: 52c66eea99a19657
2022-01-13 18:43:30 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.memusage.MemoryUsage',
'scrapy.extensions.logstats.LogStats']
2022-01-13 18:43:30 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2022-01-13 18:43:30 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2022-01-13 18:43:30 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2022-01-13 18:43:30 [scrapy.core.engine] INFO: Spider opened
2022-01-13 18:43:30 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2022-01-13 18:43:30 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2022-01-13 18:43:30 [scrapy.core.engine] INFO: Closing spider (finished)
2022-01-13 18:43:30 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'elapsed_time_seconds': 0.016834,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2022, 1, 13, 13, 13, 30, 895311),
'log_count/INFO': 10,
'memusage/max': 57552896,
'memusage/startup': 57552896,
'start_time': datetime.datetime(2022, 1, 13, 13, 13, 30, 878477)}
2022-01-13 18:43:30 [scrapy.core.engine] INFO: Spider closed (finished)
(venv) atul@vivobook:/media/atul/New Volume/Ubuntu/Code Projects/Web scraping/scrapy-tut$
Solution
The parse
method will be called for every url in start_urls
, so you don't need the begin
function (where did you get this?). If you really want to call the parse
method then do it with start_requests
.
See the example here.
With start_requests:
import scrapy
class FmSpider(scrapy.Spider):
name = 'fm'
allowed_domains = ['quotes.toscrape.com']
start_urls = ['http://quotes.toscrape.com']
def start_requests(self):
for url in self.start_urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
for quote in response.css("div.quote"):
yield {
"quote": quote.css("span.text::text").get(),
"author": quote.css("small.author::text").get()
}
And if you lose the start_requests
it won't matter. When to use start_requests
? For example if you want to add headers, proxy, anything you want to do before creating the requests.
Answered By - SuperUser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.