2013-02-01 2 views
0

URL 시퀀스를 스크랩하고 있습니다. 코드가 작동하지만 치료가 순차적으로 URL을 구문 분석하지 않습니다. 예 : 내가 url1, url2, ..., url100을 분석하려고 시도하고 있지만, scrapy url2, url10, url1 ... 등을 구문 분석합니다.치료 중 루프가 순차적으로 실행되고 있지 않습니다.

모든 URL을 구문 분석하지만 특정 URL이없는 경우 (예 : example.com/unit.aspx?b_id=10) Firefox가 이전 요청의 결과를 보여줍니다. 복제본이 없는지 확인하려면 루프가 순차적으로 URL을 파싱하고 "필요에 따라"가 아닌지 확인해야합니다.

나는 입찰 < 100 "결과는 동일하지만. 사전에

감사합니다 (아래 참조)!

def check_login_response(self, response): 
    """Check the response returned by a login request to see if we are 
    successfully logged in. 
    """ 
    if "Welcome!" in response.body: 
     self.log("Successfully logged in. Let's start crawling!") 
     print "Successfully logged in. Let's start crawling!" 
     # Now the crawling can begin.. 
     self.initialized() 
     bID=0 
     #for n in range(1,100,1): 
     while bID<100: 
      bID=bID+1 
      startURL='https://www.example.com/units.aspx?b_id=%d' % (bID) 
      request=Request(url=startURL ,dont_filter=True,callback=self.parse_add_tables,meta={'bID':bID,'metaItems':[]}) 
      # print self.metabID 
      yield request #Request(url=startURL ,dont_filter=True,callback=self.parse2) 
    else: 
     self.log("Something went wrong, we couldn't log in....Bad times :(") 
     # Something went wrong, we couldn't log in, so nothing happens. 

답변

0

볼 수 있습니다. 나는 그것이 거미줄의 나머지 부분을 보지 못했다는 것을 기초로 목적에 부합하는지 확신 할 수 없지만 여기에 당신이 간다 :

# create a list of urls to be parsed, in reverse order (so we can easily pop items off) 
crawl_urls = ['https://www.example.com/units.aspx?b_id=%s' % n for n in xrange(99,1,-1)] 

def check_login_response(self, response): 
    """Check the response returned by a login request to see if we are successfully logged in. 
    """ 
    if "Welcome!" in response.body: 
     self.log("Successfully logged in. Let's start crawling!") 
     print "Successfully logged in. Let's start crawling!" 
     # Now the crawling can begin.. 
     self.initialized() 
     return Request(url='https://www.example.com/units.aspx?b_id=1',dont_filter=True,callback=self.parse_add_tables,meta={'bID':1,'metaItems':[]}) 
    else: 
     self.log("Something went wrong, we couldn't log in....Bad times :(") 
     # Something went wrong, we couldn't log in, so nothing happens. 

def parse_add_tables(self, response): 
    # parsing code here 
    if self.crawl_urls: 
     next_url = self.crawl_urls.pop() 
     return Request(url=next_url,dont_filter=True,callback=self.parse_add_tables,meta={'bID':int(next_url[-1:]),'metaItems':[]}) 

    return items 
+0

고마워요! 이게 효과가있어. – Jmm

0

당신은 할 수 있습니다"는 또한 범위 (1101)을 n 개의과를 위해 "노력 Request 객체에서 use priority 속성을 사용한다. Scrapy는 기본적으로 URL이 DFO로 크롤링된다는 것을 보장하지만, 구문 분석 콜백 내에서 URL이 방문한 순서대로 URL을 방문하는지 보장하지는 않는다. arr를 반환하고 싶다. ay 오브 젝트가 비게 될 때까지 오브젝트가 팝되는 요청.

추가 정보를 위해 당신은 당신이 뭔가를 시도 할 수 여기

Scrapy Crawl URLs in Order

+0

답장을 보내 주셔서 감사합니다! 색인을 검색했지만이 게시물을 찾지 못했습니다. 파이썬과 스콥에 익숙하지 않아 기본 속성을 변경하는 방법에 대해 더 자세히 알아야합니다. – Jmm

관련 문제