2016-10-12 2 views
0

Ich bin ziemlich neu in Python und Scrapy, aber etwas scheint einfach nicht richtig. Gemäß der Dokumentation und Beispiel, start_requests Funktion führt Scrapy Rückkehr start_requests statt start_urls Array Variable zu verursachen.Scrapy nicht aufrufen Parse-Funktion mit Start_requests

Alles funktioniert gut mit start_urls, aber wenn ich start_requests hinzufügen, es geht nicht in Parse Funktion. Dokumentation besagt, dass Parse Methode ist

den Standard-Rückruf von Scrapy verwendet heruntergeladen Antworten zu verarbeiten, wenn ihre Anforderungen nicht angeben, einen Rückruf

aber die Parse nie ausgeführt wird, Verfolgen meiner Logger-Drucke.

Hier ist mein Code, es ist sehr kurz, da ich gerade damit herumspielen bin.

class Crawler(scrapy.Spider): 

    name = 'Hearthpwn' 
    allowed_domains = ['hearthpwn.com'] 
    storage_dir = 'C:/Users/Michal/PycharmProjects/HearthpwnCrawler/' 
    start_urls = ['http://www.hearthpwn.com/decks/645987-nzoth-warrior'] 

    def start_requests(self): 

     logging.log(logging.INFO, "Loading requests") 
     yield Request(url='http://www.hearthpwn.com/decks/646673-s31-legend-2eu-3asia-smorc-hunter') 

    def parse(self, response): 

     logging.log(logging.INFO, "parsing response") 

     filename = response.url.split("/")[-1] + '.html' 
     with open('html/' + filename, 'wb') as f: 
      f.write(response.body) 

process = CrawlerProcess({ 
    'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)' 
}) 
process.crawl(Crawler) 
process.start() 

und Druck von der Konsole:

2016-10-12 15:33:39 [scrapy] INFO: Scrapy 1.2.0 started (bot: scrapybot) 
2016-10-12 15:33:39 [scrapy] INFO: Overridden settings: {'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'} 
2016-10-12 15:33:39 [scrapy] INFO: Enabled extensions: 
['scrapy.extensions.telnet.TelnetConsole', 
'scrapy.extensions.corestats.CoreStats', 
'scrapy.extensions.logstats.LogStats'] 
2016-10-12 15:33:39 [scrapy] INFO: Enabled downloader middlewares: 
['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.chunked.ChunkedTransferMiddleware', 
'scrapy.downloadermiddlewares.stats.DownloaderStats'] 
2016-10-12 15:33:39 [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-10-12 15:33:39 [scrapy] INFO: Enabled item pipelines: 
[] 
2016-10-12 15:33:39 [scrapy] INFO: Spider opened 
2016-10-12 15:33:39 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min) 
2016-10-12 15:33:39 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023 
2016-10-12 15:33:39 [root] INFO: Loading requests 
2016-10-12 15:33:41 [scrapy] DEBUG: Redirecting (302) to <GET http://www.hearthpwn.com/decks/646673-s31-legend-2eu-3asia-smorc-hunter?cookieTest=1> from <GET http://www.hearthpwn.com/decks/646673-s31-legend-2eu-3asia-smorc-hunter> 
2016-10-12 15:33:41 [scrapy] DEBUG: Redirecting (302) to <GET http://www.hearthpwn.com/decks/646673-s31-legend-2eu-3asia-smorc-hunter> from <GET http://www.hearthpwn.com/decks/646673-s31-legend-2eu-3asia-smorc-hunter?cookieTest=1> 
2016-10-12 15:33:41 [scrapy] DEBUG: Filtered duplicate request: <GET http://www.hearthpwn.com/decks/646673-s31-legend-2eu-3asia-smorc-hunter> - no more duplicates will be shown (see DUPEFILTER_DEBUG to show all duplicates) 
2016-10-12 15:33:41 [scrapy] INFO: Closing spider (finished) 
2016-10-12 15:33:41 [scrapy] INFO: Dumping Scrapy stats: 
{'downloader/request_bytes': 655, 
'downloader/request_count': 2, 
'downloader/request_method_count/GET': 2, 
'downloader/response_bytes': 1248, 
'downloader/response_count': 2, 
'downloader/response_status_count/302': 2, 
'dupefilter/filtered': 1, 
'finish_reason': 'finished', 
'finish_time': datetime.datetime(2016, 10, 12, 13, 33, 41, 740724), 
'log_count/DEBUG': 4, 
'log_count/INFO': 8, 
'scheduler/dequeued': 2, 
'scheduler/dequeued/memory': 2, 
'scheduler/enqueued': 2, 
'scheduler/enqueued/memory': 2, 
'start_time': datetime.datetime(2016, 10, 12, 13, 33, 39, 441736)} 
2016-10-12 15:33:41 [scrapy] INFO: Spider closed (finished) 

Vielen Dank für alle führt.

Antwort

1

Mit dont_merge_cookies Attribute im meta Wörterbuch würde dieses Problem lösen.

def start_requests(self): 

     logging.log(logging.INFO, "Loading requests") 
     yield Request(url='http://www.hearthpwn.com/decks/646673-s31-legend-2eu-3asia-smorc-hunter', 
         meta={'dont_merge_cookies': True}) 
+0

Dank für Sie und @Granitosaurus! Während diese Antwort für das, was ich beabsichtigte, ausreichend war, gaben mir beide einen interessanten Einblick. Ich endete mit einem Redirect-style-Link, wo es einfach war, den Namen in das ursprüngliche Formular zu parsen und zu speichern. –

0
2016-10-12 15:33:41 [scrapy] DEBUG: Redirecting (302) to <GET http://www.hearthpwn.com/decks/646673-s31-legend-2eu-3asia-smorc-hunter?cookieTest=1> from <GET http://www.hearthpwn.com/decks/646673-s31-legend-2eu-3asia-smorc-hunter> 
2016-10-12 15:33:41 [scrapy] DEBUG: Redirecting (302) to <GET http://www.hearthpwn.com/decks/646673-s31-legend-2eu-3asia-smorc-hunter> from <GET http://www.hearthpwn.com/decks/646673-s31-legend-2eu-3asia-smorc-hunter?cookieTest=1> 
2016-10-12 15:33:41 [scrapy] DEBUG: Filtered duplicate request: <GET http://www.hearthpwn.com/decks/646673-s31-legend-2eu-3asia-smorc-hunter> - no more duplicates will be shown (see DUPEFILTER_DEBUG to show all duplicates) 

Was hier passiert, ist, dass die Website, die Sie mehrmals umleitet und Sie am Ende die gleiche URL kriecht zweimal, weil es. Scrapy Spider hat standardmäßig eine Middleware, die doppelte Anfragen herausfiltert. Daher müssen Sie den Parameter dont_filter auf True setzen, wenn Sie das Request-Objekt erstellen, um diese Middleware zu ignorieren.

z.B .:

def start_requests(self): 
    yield ('http://scrapy.org', dont_filter=True)