2016-07-25 3 views
0

나는 치료를 사용하여 here에서 결과를 긁어 내려고합니다. 문제는 '더 많은 결과로드'탭을 클릭하기 전까지는 모든 클래스가 페이지에 표시되지 않는다는 것입니다. Scrapy의 다음 페이지로 어떻게 이동할 수 있습니까

문제

여기에서 볼 수있다 :

enter image description here

내 코드는 다음과 같습니다 :이 웹 사이트에 대한

class ClassCentralSpider(CrawlSpider): 
    name = "class_central" 
    allowed_domains = ["www.class-central.com"] 
    start_urls = (
     'https://www.class-central.com/courses/recentlyAdded', 
    ) 
    rules = (
     Rule(
      LinkExtractor(
       # allow=("index\d00\.html",), 
       restrict_xpaths=('//div[@id="show-more-courses"]',) 
      ), 
      callback='parse', 
      follow=True 
     ), 
    ) 

def parse(self, response): 
    x = response.xpath('//span[@class="course-name-text"]/text()').extract() 
    item = ClasscentralItem() 
    for y in x: 
     item['name'] = y 
     print item['name'] 

    pass 
+0

두 번째 페이지의 URL은 어떤 모양입니까? 그것의 경우처럼 www.website.com/Recently_Added/2 그때는 정말 간단한 해결책이 될 것입니다. 또는 실제로 더 많은 결과를로드하는 데 나타나는 데이터를 가져 오려고합니까? – SAMO

+0

작동하지 않습니다. 나는 2 페이지의 URL을 얻는 방법을 모르거나 [다음에 ..를로드하십시오] – Yato

+0

예를 들어 보겠습니다. URL이 명백한 패턴으로 바뀌면 그것을 활용할 수 있다고 말하고 있습니다. 그러면 결과를 '더 많은 결과로드'탭에서 얻으려고하는 것입니다. – SAMO

답변

1

두 번째 페이지는 AJAX 호출을 통해 생성 된 것으로 보인다.

https://www.class-central.com/maestro/courses/recentlyAdded?page=2&_=1469471093134

에서 JSON 파일을 검색하는 것 같다이 경우 firebug network tab

지금은 그 URL을 보인다 : 어떤 브라우저 검사 도구의 네트워크 탭에 보면, 당신은 같은 것을 볼 수 있습니다

# so you just need to load it up with 
data = json.loads(response.body) 
# and convert it to scrapy selector - 
sel = Selector(text=data['table']) 
,536,913,632 : https://www.class-central.com/maestro/courses/recentlyAdded?page=2
반환 JSON은 다음 페이지에 대한 HTML 코드가 포함되어 매개 변수 _=1469471093134 아무것도 그래서 그냥 멀리에 다시 트림 수 없습니다 않습니다 10

코드에서이를 복제하려면 다음과 같이 시도하십시오.

from w3lib.url import add_or_replace_parameter 
def parse(self, response): 
    # check if response is json, if so convert to selector 
    if response.meta.get('is_json',False): 
     # convert the json to scrapy.Selector here for parsing 
     sel = Selector(text=json.loads(response.body)['table']) 
    else: 
     sel = Selector(response) 
    # parse page here for items 
    x = sel.xpath('//span[@class="course-name-text"]/text()').extract() 
    item = ClasscentralItem() 
    for y in x: 
     item['name'] = y 
     print(item['name']) 
    # do next page 
    next_page_el = respones.xpath("//div[@id='show-more-courses']") 
    if next_page_el: # there is next page 
     next_page = response.meta.get('page',1) + 1 
     # make next page url 
     url = add_or_replace_parameter(url, 'page', next_page) 
     yield Request(url, self.parse, meta={'page': next_page, 'is_json': True) 
+0

응답 = json.loads (response.load) 이것을 사용하여 응답을 json에서 selector로 바꾸십시오. 마지막에 '}'가 끝나면 Request (url, self.parse, meta = { 'page': next_page, 'is_json': True})를 내 보냅니다. – Yato

+0

나는 당신의 구문 분석이하지만 페이지 매김을 통해 무엇을하는지에 대한 나의 대답에서'parse()'메소드를 편집했다. 나는 코드를 테스트하지 않았지만, 당신이 직접 발견한다면 몇 가지 오타를 수정할 수 있다고 생각한다. :) – Granitosaurus

+0

또 다른 오류를 수정할 수있다. 아마도. : D. 고맙습니다 !!! – Yato

관련 문제