2017-11-11 10 views
0

私はScrapyを初めて使いました。現在、Tor darknetのフォーラムをクロールするCrawlSpiderを作成しようとしています。現在、私のCrawlSpiderコードは次のとおりです。私の治療用CrawlSpiderで相対パスを絶対パスに変換するにはどうすればよいですか?

:フォーラムは、相対パスを使用しています

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 

ので、私は「../」私は自分のコードを実行したときしかし、私はまだrecieving午前を削除するには、カスタムprocess_linksを作成しようとしました

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) 

ご覧のとおり、私はまだ悪いパスのために400のエラーが発生しています。私のコードがリンクから "../"を削除しないのはなぜですか?

ありがとうございます!

答えて

0

makeAbsolutePathsはスパイダークラスの一部ではない可能性があります。 The documentation states

process_links is a callable, or a string (in which case a method from the spider object with that name will be used)

あなたはmakeAbsolutePathsselfを使用していなかったので、私はそれがインデントのエラーではありませんと仮定します。 makeAbsolutePathsには他にもいくつかのエラーがあります。それがこのエラーを生成します

import scrapy 
from scrapy.spiders import CrawlSpider, Rule 
from scrapy.linkextractors import LinkExtractor 


class HiddenAnswersSpider(CrawlSpider): 
    name = 'ha' 
    start_urls = ['file:///home/user/testscrapy/test.html'] 
    allowed_domains = [] 
    rules = (
      Rule(LinkExtractor(allow=(r'.*')), follow=True, process_links='makeAbsolutePath'), 
      ) 

    def makeAbsolutePath(self, links): 
     print(links) 
     for i in range(links): 
      links[i] = links[i].replace("../","") 
     return links 

:私たちは、この状態にコードを修正した場合

TypeError: 'list' object cannot be interpreted as an integer 

len()への呼び出しがrangeへの呼び出しで使用されなかったとrangeのみで動作させることができるので、これは、あります整数。これは、番号を望んでいると、この問題を修正した後、あなたに0からこの番号の範囲を与えるマイナス1

だろう、それはエラーを与える:

AttributeError: 'Link' object has no attribute 'replace' 

これは - linksがある - あなたが考えと違っているのでhref=""属性の内容を含む文字列のリストではありません。代わりに、Linkオブジェクトのリストです。

linksの内容をmakeAbsolutePathの中に出力し、何かをする必要があれば参照してください。私の意見では、実際のフォルダレベルのない..オペレータ(URLは/questionsで、/questions/ではない)をサイトが使用していても、治療はドメインレベルに達した時点ですでに..オペレータの解決を停止する必要があるため、リンクはhttp://answerstedhctbek.onion/<number>/<title>を指す必要があります。

どういうわけか、このような:

def makeAbsolutePath(self, links): 
     for i in range(len(links)): 
      print(links[i].url) 

     return [] 

(ここでは空のリストを返すことは、あなたにクモが停止し、コンソール出力を確認することができるという利点を提供します)

あなたが見つける場合は、URLをあなたはurl属性を介してそれらのいくつかの作業を行うことができ、実際に間違っている:

links[i].url = 'http://example.com' 
+0

Aufziehvogel、それは最終的に正しく、あなたのおかげで働いています! makeAbsolutePathのパラメータとして 'self'を追加するまで、上記のエラーを受け取れませんでした。だから、あなたが言及した他のすべての決議を含めて、「自己」を加えることはこれを解決しました。URLはまだ間違っていましたが、私は単にラインリンク[i] .url = links [i] .url.replace( '../'、 '')を含めることができました。 – ToriTompkins

関連する問題