I want to use scrapy for crawling web pages. Is there a way to pass the start URL from the terminal itself?
It is given in the documentation that either the name of the spider or the URL can be given, but when i given the url it throws an error:
//name of my spider is example, but i am giving url instead of my spider name(It works fine if i give spider name).
scrapy crawl example.com
ERROR:
File "/usr/local/lib/python2.7/dist-packages/Scrapy-0.14.1-py2.7.egg/scrapy/spidermanager.py", line 43, in create raise KeyError("Spider not found: %s" % spider_name) KeyError: 'Spider not found: example.com'
How can i make scrapy to use my spider on the url given in the terminal??
Sjaak Trekhaak has the right idea and here is how to allow multiples:
Use scrapy parse command. You can parse a url with your spider. url is passed from command.
http://doc.scrapy.org/en/latest/topics/commands.html#parse
This is an extension to the approach given by Sjaak Trekhaak in this thread. The approach as it is so far only works if you provide exactly one url. For example, if you want to provide more than one url like this, for instance:
then Scrapy (I'm using the current stable version 0.14.4) will terminate with the following exception:
However, you can circumvent this problem by choosing a different variable for each start url, together with an argument that holds the number of passed urls. Something like this:
You can then do the following in your spider:
This is a somewhat ugly hack but it works. Of course, it's tedious to explicitly write down all command line arguments for each url. Therefore, it makes sense to wrap the
scrapy crawl
command in a Python subprocess and generate the command line arguments in a loop or something.Hope it helps. :)
I'm not really sure about the commandline option. However, you could write your spider like this.
And start it like:
scrapy crawl my_spider -a start_url="http://some_url"
An even easier way to allow multiple url-arguments than what Peter suggested is by giving them as a string with the urls separated by a comma, like this:
In the spider you would then simply split the string on ',' and get an array of urls:
You can also try this:
It will open a window in browser of requested URL.