。。This is my spider:

 from scrapy.spider import BaseSpider
 class 192.168.0.185_Spider(BaseSpider):
      name = "192.168.0.185"
      allowed_domains = ["192.168.0.185"]
      start_urls = ["http://192.168.0.185/"]

      def parse(self, response):
          print "Test:", response.headers

And then in the same directory as my spider I'd execute this shell command to run the spider:
scrapy crawl 192.168.0.185

我得到了一个非常难看的错误信息:
 2012-02-10 20:55:18-0600 [scrapy] INFO: Scrapy 0.14.0 started (bot: tutorial)
 2012-02-10 20:55:18-0600 [scrapy] DEBUG: Enabled extensions: LogStats,
 TelnetConsole,     CloseSpider, WebService, CoreStats, MemoryUsage, SpiderState
 2012-02-10 20:55:18-0600 [scrapy] DEBUG: Enabled downloader middlewares:
 HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware,
 DefaultHeadersMiddleware, RedirectMiddleware, CookiesMiddleware,
 HttpCompressionMiddleware, ChunkedTransferMiddleware, DownloaderStats
 2012-02-10 20:55:18-0600 [scrapy] DEBUG: Enabled spider middlewares:
 HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware,
 DepthMiddleware 2012-02-10 20:55:18-0600 [scrapy] DEBUG: Enabled item pipelines:
 Traceback (most recent call last): File "/usr/bin/scrapy", line 5, in <module>
 pkg_resources.run_script('Scrapy==0.14.0', 'scrapy')
 File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 467, in run_script
 self.require(requires)[0].run_script(script_name, ns)
 File "/usr/lib/python2.6/site-packages/pkg_resources.py", line 1200, in run_script
 execfile(script_filename, namespace, namespace)
 File "/usr/lib/python2.6/site-packages/Scrapy-0.14.0-py2.6.egg/EGG-INFO/scripts
 /scrapy", line 4, in <module>
 execute()
 File "/usr/lib/python2.6/site-packages/Scrapy-0.14.0-py2.6.egg/scrapy/cmdline.py",
 line 132, in execute
 _run_print_help(parser, _run_command, cmd, args, opts)
 File "/usr/lib/python2.6/site-packages/Scrapy-0.14.0-py2.6.egg/scrapy/cmdline.py",
 line 97, in _run_print_help func(*a, **kw)
 File "/usr/lib/python2.6/site-packages/Scrapy-0.14.0-py2.6.egg/scrapy/cmdline.py",
 line 139, in _run_command cmd.run(args, opts)
 File "/usr/lib/python2.6/site-packages/Scrapy-0.14.0-py2.6.egg/scrapy/commands
 /crawl.py", line 43, in run
 spider = self.crawler.spiders.create(spname, **opts.spargs)
 File "/usr/lib/python2.6/site-packages/Scrapy-0.14.0-py2.6.egg/scrapy
 /spidermanager.py", line 43, in create
 raise KeyError("Spider not found: %s" % spider_name)
 KeyError: 'Spider not found: 192.168.0.185'

。这个效果不错。有人知道这是什么交易吗?如何让Scrapy通过IP地址而不是域名来抓取网站?
from scrapy.spider import BaseSpider
class facebook_Spider(BaseSpider):
     name = "facebook"
     allowed_domains = ["facebook.com"]
     start_urls = ["http://www.facebook.com/"]


     def parse(self, response):
         print "Test:", response.headers

最佳答案

class 192.168.0.185_Spider(BaseSpider):
    ...

。See documentation Identifiers and keywords
可以使用正确的名称创建此蜘蛛:
$ scrapy startproject testproj
$ cd testproj
$ scrapy genspider testspider 192.168.0.185
  Created spider 'testspider' using template 'crawl' in module:
    testproj.spiders.testspider

蜘蛛的定义如下:
class TestspiderSpider(CrawlSpider):
    name = 'testspider'
    allowed_domains = ['192.168.0.185']
    start_urls = ['http://www.192.168.0.185/']
    ...

或许你应该从www中删除start_urls。To start crawling, use spider name instead host:
$ scrapy crawl testspider

关于python - Scrapy通过IP地址抓取本地网站,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9237545/

10-09 01:38