Ich bin neu in Scrapy und ich versuche gerade einen CrawlSpider zu schreiben, der ein Forum auf dem Tor Darknet crawlen wird. Derzeit ist mein CrawlSpider Code ist:Wie kann ich mit meinem scrapy CrawlSpider relative Pfade in absolute Pfade umwandeln?
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class HiddenAnswersSpider(CrawlSpider):
name = 'ha'
start_urls = ['http://answerstedhctbek.onion/questions']
allowed_domains = ['http://answerstedhctbek.onion', 'answerstedhctbek.onion']
rules = (
Rule(LinkExtractor(allow=(r'answerstedhctbek.onion/\d\.\*', r'https://answerstedhctbek.onion/\d\.\*')), follow=True, process_links='makeAbsolutePath'),
Rule(LinkExtractor(allow=()), follow=True, process_links='makeAbsolutePath')
)
def makeAbsolutePath(links):
for i in range(links):
links[i] = links[i].replace("../","")
return links
Da das Forum relativen Pfad verwendet, habe ich versucht, eine benutzerdefinierte process_links zu erstellen, um die „../“ jedoch zu entfernen, wenn ich meinen Code ausführen ich noch recieving:
2017-11-11 14:46:46 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <400 http://answerstedhctbek.onion/../badges>: HTTP status code is not handled or not allowed
2017-11-11 14:46:46 [scrapy.core.engine] DEBUG: Crawled (400) <GET http://answerstedhctbek.onion/../general-guidelines> (referer: http://answerstedhctbek.onion/questions)
2017-11-11 14:46:47 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <400 http://answerstedhctbek.onion/../general-guidelines>: HTTP status code is not handled or not allowed
2017-11-11 14:46:47 [scrapy.core.engine] DEBUG: Crawled (400) <GET http://answerstedhctbek.onion/../contact-us> (referer: http://answerstedhctbek.onion/questions)
2017-11-11 14:46:47 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <400 http://answerstedhctbek.onion/../contact-us>: HTTP status code is not handled or not allowed
2017-11-11 14:46:48 [scrapy.core.engine] DEBUG: Crawled (400) <GET http://answerstedhctbek.onion/../questions?sort=hot> (referer: http://answerstedhctbek.onion/questions)
2017-11-11 14:46:48 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <400 http://answerstedhctbek.onion/../questions?sort=hot>: HTTP status code is not handled or not allowed
2017-11-11 14:46:48 [scrapy.core.engine] DEBUG: Crawled (400) <GET http://answerstedhctbek.onion/../questions?sort=votes> (referer: http://answerstedhctbek.onion/questions)
Wie Sie sehen können, bekomme ich immer noch 400 Fehler aufgrund des schlechten Pfades. Warum entfernt mein Code die "../" nicht von den Links?
Danke!
Aufziehvogel, es endlich richtig funktioniert dank an Sie! Ich konnte keinen der oben genannten Fehler erhalten, bis ich "self" als Parameter in makeAbsolutePath hinzugefügt habe. Das Hinzufügen von "Selbst" einschließlich aller anderen von Ihnen genannten Auflösungen hat das Problem gelöst.Die URLs waren immer noch falsch, aber ich war in der Lage, einfach die Linie Links [i] .url = Links [i] .url.replace ('../', '') – ToriTompkins