2013-03-19 3 views
10

전체 웹 사이트를 크롤링 할 수 없으며 표면에서 스컬이 단지 크롤링합니다. 더 크롤링하고 싶습니다. 지난 5 ~ 6 시간 동안 인터넷 검색을했는데 도움이되지 않았습니다. 아래 코드 :치료 - 전체 웹 사이트를 크롤링

from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 
from scrapy.selector import HtmlXPathSelector 
from scrapy.item import Item 
from scrapy.spider import BaseSpider 
from scrapy import log 

class ExampleSpider(CrawlSpider): 
    name = "example.com" 
    allowed_domains = ["example.com"] 
    start_urls = ["http://www.example.com/"] 
    rules = [Rule(SgmlLinkExtractor(allow=()), 
        follow=True), 
      Rule(SgmlLinkExtractor(allow=()), callback='parse_item') 
    ] 
    def parse_item(self,response): 
     self.log('A response from %s just arrived!' % response.url) 

도와주세요 !!!! 링크 만족이 적용됩니다 규칙이 될 첫 번째 규칙을 의미

감사합니다, Abhiram

+1

방금 ​​stackoverflow에 대한 코드를 시도했는데 - 내 ip가 금지되었습니다. 그것은 확실히 작동합니다! :) – alecxe

+0

@Alexander - 내가 더 많은 것을 디버깅하도록 고무시키는 소리 :) :) ... IP 금지 메이트에서 미안 해요! –

+0

example.com을 실제로 크롤링하려고합니까? 당신은 그것이 진짜 웹 사이트가 아니라는 것을 알고 있습니다. –

답변

6

규칙 단락은, (콜백) 두 번째 규칙은 호출되지 않습니다.

이에 규칙을 변경

다음 start_urls을 구문 분석 할 때

rules = [Rule(SgmlLinkExtractor(), callback='parse_item', follow=True)] 
+0

@All - 작동했습니다 ... Steven이 옳았으며 도움에 감사드립니다! 하지만 전체 사이트를 크롤링 할 수 없으며 단지 80 개의 홀수 페이지를 크롤링 할 수있었습니다. 수정해야 할 사항이 있습니까? 다음은 제 작업 버전입니다. (규칙 (SgmlLinkExtractor (allow = ('pages /')), Follow = True, callback = 'parse_item')) –

+0

안녕하세요! 이 문제에 도움을 주시겠습니까? http : //stackoverflow.com/questions/31630771/scrapy-linkextractor-duplicating – yukclam9

+0

@Steven Almeroth 안녕하세요 스티븐 당신이 도와 드릴까요? http://stackoverflow.com/questions/37717122/grabbed -data-from-a-given-url-and-put-it-a-file-using-scrapy 규칙의 변화를 시도했지만 저에게 효과적이지 않았습니다. – nand

2

, 깊은 URL은 태그 href에 의해 분석 될 수있다. 그런 다음 더 깊은 요청은 parse() 함수에서 얻을 수 있습니다. Here is a simple example. 가장 중요한 소스 코드는 다음과 같습니다.

from scrapy.spiders import Spider 
from tutsplus.items import TutsplusItem 
from scrapy.http import Request 
import re 

class MySpider(Spider): 
    name   = "tutsplus" 
    allowed_domains = ["code.tutsplus.com"] 
    start_urls  = ["http://code.tutsplus.com/"] 

    def parse(self, response): 
     links = response.xpath('//a/@href').extract() 

     # We stored already crawled links in this list 
     crawledLinks = [] 

     # Pattern to check proper link 
     # I only want to get tutorial posts 
     linkPattern = re.compile("^\/tutorials\?page=\d+") 

     for link in links: 
     # If it is a proper link and is not checked yet, yield it to the Spider 
      if linkPattern.match(link) and not link in crawledLinks: 
       link = "http://code.tutsplus.com" + link 
       crawledLinks.append(link) 
       yield Request(link, self.parse) 

     titles = response.xpath('//a[contains(@class, "posts__post-title")]/h1/text()').extract() 
     for title in titles: 
      item = TutsplusItem() 
      item["title"] = title 
      yield item 
관련 문제