为什么我的基本scrapy请求没有响应?

时间:2017-08-10 03:58:33

标签: python scrapy response scrapy-shell

我是scrapy的新手并尝试提交表单并从https://www.fbo.gov/index?s=opportunity&tab=search&mode=list获取回复。

当我使用scrapy shell时:

scrapy shell "https://www.fbo.gov/index?s=opportunity&tab=search&mode=list"

它打开shell但不包含响应对象。运行

print(response)

不返回。 我尝试过使用" https://www.fbo.gov"和其他变化,但似乎没有任何作用。我遵循的例子使用" http://quotes.toscrape.com/page/1/"它工作正常。

为什么在使用其他网址时我没有得到任何回复?它与https有关吗?由于链接包含表单,我是否需要使用FormRequest来获取响应?我认为它至少会返回表单的html。我打算检查'提交后的各种复选框。

提前感谢您的帮助!

日志:

2017-08-09 21:45:43 [scrapy.utils.log] INFO: Scrapy 1.4.0 started (bot: fbg)
2017-08-09 21:45:43 [scrapy.utils.log] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'fbg.spiders', 'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES': ['fbg.spiders'], 'DUPEFILTER_CLASS': 'scrapy.dupefilters.BaseDupeFilter', 'BOT_NAME': 'fbg', 'LOGSTATS_INTERVAL': 0}
2017-08-09 21:45:44 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.memusage.MemoryUsage',
 'scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.telnet.TelnetConsole']
2017-08-09 21:45:44 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
 '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']
2017-08-09 21:45:45 [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']
2017-08-09 21:45:45 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2017-08-09 21:45:45 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2017-08-09 21:45:45 [scrapy.core.engine] INFO: Spider opened
2017-08-09 21:45:45 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.fbo.gov/robots.txt> (referer: None)
2017-08-09 21:45:45 [scrapy.downloadermiddlewares.robotstxt] DEBUG: Forbidden by robots.txt: <GET https://www.fbo.gov/index?s=opportunity&tab=search&mode=list>
[s] Available Scrapy objects:
[s]   scrapy     scrapy module (contains scrapy.Request, scrapy.Selector, etc)
[s]   crawler    <scrapy.crawler.Crawler object at 0x1101058d0>
[s]   item       {}
[s]   request    <GET https://www.fbo.gov/index?s=opportunity&tab=search&mode=list>
[s]   settings   <scrapy.settings.Settings object at 0x1101059e8>
[s] Useful shortcuts:
[s]   fetch(url[, redirect=True]) Fetch URL and update local objects (by default, redirects are followed)
[s]   fetch(req)                  Fetch a scrapy.Request and update local objects 
[s]   shelp()           Shell help (print this help)
[s]   view(response)    View response in a browser

1 个答案:

答案 0 :(得分:0)

你的日志说:

2017-08-09 21:45:45 [scrapy.downloadermiddlewares.robotstxt] DEBUG: Forbidden by robots.txt: <GET https://www.fbo.gov/index?s=opportunity&tab=search&mode=list>

似乎您已将ROBOTSTXT_ENABLED设置为True,因此您的请求会被过滤掉。尝试在项目中禁用它或运行scrapy shell url -s ROBOTSTXT_ENABLED=0

当你打开一个新终端时它起作用的原因&#34;是你可能从非项目目录启动shell,scrapy不再从你的项目中获取此设置。