2016-11-01 8 views
1

CSSセレクタを使用してhttp://www.bschool.careers360.com/search/all/bangaloreから大学名を抽出しようとしていますが、データが抽出されません。 "ROBOTSTXT_OBEY = False"が設定されています。変更後、私のコードは以下の通りです。が、結果は同じpython scrapy cssセレクタ名抽出が機能しない

import scrapy 

class BloreSpider(scrapy.Spider): 
    name = 'blore' 
    start_urls = ['http://www.engineering.careers360.com/search/college/bangalore'] 

    def parse(self, response): 
     for quote in response.css('div.title'): 
      yield { 
       'author': quote.xpath('.//a/text()').extract_first(), 
      } 

     next_page = response.css('li.pager-next a::attr("href")').extract_first() 
     if next_page: 
      next_page = response.urljoin(next_page) 
      yield scrapy.Request(next_page, callback=self.parse) 

をままで、ログが

2016-11-02 11:49:47 [scrapy] INFO: Scrapy 1.1.1 started (bot: google) 
2016-11-02 11:49:47 [scrapy] INFO: Overridden settings: {'FEED_URI': 'item.csv', 
'BOT_NAME': 'google', 'NEWSPIDER_MODULE': 'google.spiders', 'FEED_FORMAT': 'csv 
', 'SPIDER_MODULES': ['google.spiders']} 
2016-11-02 11:49:47 [scrapy] INFO: Enabled extensions: 
['scrapy.extensions.telnet.TelnetConsole', 
'scrapy.extensions.corestats.CoreStats', 
'scrapy.extensions.feedexport.FeedExporter', 
'scrapy.extensions.logstats.LogStats'] 
2016-11-02 11:49:47 [scrapy] INFO: Enabled downloader middlewares: 
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware', 
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware', 
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware', 
'scrapy.downloadermiddlewares.retry.RetryMiddleware', 
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware', 
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware', 
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware', 
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware', 
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware', 
'scrapy.downloadermiddlewares.chunked.ChunkedTransferMiddleware', 
'scrapy.downloadermiddlewares.stats.DownloaderStats'] 
2016-11-02 11:49:47 [scrapy] INFO: Enabled spider middlewares: 
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware', 
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware', 
'scrapy.spidermiddlewares.referer.RefererMiddleware', 
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware', 
'scrapy.spidermiddlewares.depth.DepthMiddleware'] 
2016-11-02 11:49:47 [scrapy] INFO: Enabled item pipelines: 
[] 
2016-11-02 11:49:47 [scrapy] INFO: Spider opened 
2016-11-02 11:49:47 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 i 
tems (at 0 items/min) 
2016-11-02 11:49:47 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023 
2016-11-02 11:49:47 [scrapy] DEBUG: Crawled (403) <GET http://www.engineering.ca 
reers360.com/search/college/bangalore> (referer: None) 
2016-11-02 11:49:47 [scrapy] DEBUG: Ignoring response <403 http://www.engineerin 
g.careers360.com/search/college/bangalore>: HTTP status code is not handled or n 
ot allowed 
2016-11-02 11:49:47 [scrapy] INFO: Closing spider (finished) 
2016-11-02 11:49:47 [scrapy] INFO: Dumping Scrapy stats: 
{'downloader/request_bytes': 252, 
'downloader/request_count': 1, 
'downloader/request_method_count/GET': 1, 
'downloader/response_bytes': 2473, 
'downloader/response_count': 1, 
'downloader/response_status_count/403': 1, 
'finish_reason': 'finished', 
'finish_time': datetime.datetime(2016, 11, 2, 6, 19, 47, 644833), 
'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(2016, 11, 2, 6, 19, 47, 403819)} 
2016-11-02 11:49:47 [scrapy] INFO: Spider closed (finished) 

答えて

0

あなたのxpathがあなたのquoteノードは、他の言葉であなたが//.を追加する必要があり、あなたの相対的であることが必要です。

これを試してみてください:

def parse(self, response): 
    for quote in response.css('div.title'): 
     yield { 
      #'author': quote.xpath('//a/text()').extract_first(), 
      #      ^
      'author': quote.xpath('.//a/text()').extract_first(), 
     } 

    next_page = response.css('li.pager-next a::attr("href")').extract_first() 
    # if next_page is not None: 
    if next_page: # you can also just do this 
     next_page = response.urljoin(next_page) 
     yield scrapy.Request(next_page, callback=self.parse) 

編集:あなたの提供、ログを見ては、robots.txtのを取得しようとしたとき、あなたが404を得るように思えます。試しましたが、ROBOTS_TXT_OBEY = Falsesettings.py

+1

に設定してみてください。 –

+0

@punithbmええ、私はあなたのログを見ました。あなたが禁止されているか、それともあなたがサイトのrobots.txtに従っているからです。これを避ける方法は私の編集を参照してください。 – Granitosaurus

+1

'ROBOTSTXT_OBEY = False'が設定されていますが、結果に変更はありません。 –

関連する問題