问题
I was following this link and i was able to run a basespider successfully.
How ever when i tried using the same with a crawlspider, i was not getting any output.
My spider is as follows:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.http import request
from scrapy.selector import HtmlXPathSelector
from medsynergies.items import MedsynergiesItem
class medsynergiesspider(CrawlSpider):
name="medsynergies"
allowed_domains=['msi-openhire.silkroad.com/epostings/']
start_urls=['https://msi-openhire.silkroad.com/epostings/index.cfm?fuseaction=app.jobsearch']
rules=(
Rule(SgmlLinkExtractor(allow=("jobsearch",))),
Rule(SgmlLinkExtractor(allow=("jobinfo&jobid",)),callback="parse_job",follow=True),
)
def parse_job(self, response):
hxs=HtmlXPathSelector(response)
titles=hxs.select('//*[@id="jobDesciptionDiv"]')
items = []
for titles in titles:
item=MedsynergiesItem()
item['job_id']=response.url
item['title']=titles.select('normalize-space(//*[@id="jobTitleDiv"]/text())').extract()
item['tracking_code']=titles.select('normalize-space(//*[@id="trackCodeDiv"]/text())').extract()
item['job_description']=titles.select('.//p/text()').extract()
item['responsibilities']=titles.select('.//ul/li/text()').extract()
item['required_skills']=titles.select('normalize-space(//*[@id="jobRequiredSkillsDiv"]/ul)').extract()
item['job_location']=titles.select('normalize-space(//*[@id="jobPositionLocationDiv"]/text())').extract()
item['position_type']=titles.select('normalize-space(//*[@id="translatedJobPostingTypeDiv"]/text())').extract()
items.append(item)
print items
return items
The output i was getting was as follows:
abhishek@abhishek-VirtualBox:~/projects/medsynergies$ scrapy crawl medsynergies > output.txt
2014-02-24 17:18:07-0700 [scrapy] INFO: Scrapy 0.22.1 started (bot: medsynergies)
2014-02-24 17:18:07-0700 [scrapy] INFO: Optional features available: ssl, http11, boto, django
2014-02-24 17:18:07-0700 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'medsynergies.spiders', 'SPIDER_MODULES': ['medsynergies.spiders'], 'BOT_NAME': 'medsynergies'}
2014-02-24 17:18:07-0700 [scrapy] INFO: Enabled extensions: LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState
2014-02-24 17:18:07-0700 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
2014-02-24 17:18:07-0700 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2014-02-24 17:18:07-0700 [scrapy] INFO: Enabled item pipelines:
2014-02-24 17:18:07-0700 [medsynergies] INFO: Spider opened
2014-02-24 17:18:07-0700 [medsynergies] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2014-02-24 17:18:07-0700 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:6023
2014-02-24 17:18:07-0700 [scrapy] DEBUG: Web service listening on 0.0.0.0:6080
2014-02-24 17:18:16-0700 [medsynergies] DEBUG: Crawled (200) <GET https://msi-openhire.silkroad.com/epostings/index.cfm?fuseaction=app.jobinfo&jobid=1284&source=ONLINE&JobOwner=992700&company_id=16616&version=1&byBusinessUnit=NULL&bycountry=0&bystate=0&byRegion=&bylocation=NULL&keywords=&byCat=NULL&proximityCountry=&postalCode=&radiusDistance=&isKilometers=&tosearch=yes> (referer: None) ['partial']
2014-02-24 17:18:16-0700 [medsynergies] INFO: Closing spider (finished)
2014-02-24 17:18:16-0700 [medsynergies] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 496,
'downloader/request_count': 1,
'downloader/request_method_count/GET': 1,
'downloader/response_bytes': 6637,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2014, 2, 25, 0, 18, 16, 171093),
'log_count/DEBUG': 3,
'log_count/INFO': 7,
'response_received_count': 1,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2014, 2, 25, 0, 18, 7, 451729)}
2014-02-24 17:18:16-0700 [medsynergies] INFO: Spider closed (finished)
Please let me know where i am doing the mistake. If my rules are too strict, how do i change them such that they scrape?
回答1:
It is not about your rules
, the problem is in allowed_domains
:
allowed_domains = ['msi-openhire.silkroad.com']
Also, note that your parse()
function contains a serious problem. You are overwriting the titles
. Replace:
for titles in titles:
with:
for title in titles:
And every item field definition should use title
.
Also, note that HtmlXPathSelector
and select()
are deprecated, use Selector and xpath().
来源:https://stackoverflow.com/questions/24289854/scrapy-not-giving-any-output