2017-11-11 8 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 

때문에, 나는 "../"내 코드를 실행하면 그러나 나는 아직도 잡 오전를 제거하기 위해 사용자 정의 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)

당신은 makeAbsolutePaths에서 self 사용하지 않은, 그래서 그것이 들여 쓰기 오류가 아닙니다 가정합니다. 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 만에 작동 할 수 있기 때문이다 정수. - 당신이 생각과는 달리하기 때문에 - links이다

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

이것은 : 그것은, 그것은 오류를 줄 것이다 번호를 원하고이 문제를 해결 한 후 1

마이너스이 숫자 0에서 당신에게 범위를 줄 것이다 href="" 속성의 내용이 들어있는 문자열 목록이 아닙니다. 대신, Link 개체의 목록입니다.

links의 내용을 makeAbsolutePath 안에 출력하고 무엇이든해야한다면 참조하십시오. 제 생각에는 주소가 이고 /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