2017-12-09 11 views
0

나는 거미가 긁는 페이지에 도착하기 전에 3 페이지를 지나는 거미를 만들고자한다. 그러나 쉘에서 응답을 테스트했지만, 함께 작동하지 않는 이유와 확실하지 않습니다. 아래거미가 링크를 따라갈 수 없다 - 치료

내 코드 :

# -*- coding: utf-8 -*- 
import scrapy 


class CollegiateSpider(scrapy.Spider): 
    name = 'Collegiate' 
    allowed_domains = ['collegiate-ac.com/uk-student-accommodation'] 
    start_urls = ['http://collegiate-ac.com/uk-student-accommodation/'] 

    # Step 1 - Get the area links 

    def parse(self, response): 
     for city in response.xpath('//*[@id="top"]/div[1]/div/div[1]/div/ul/li/a/text').extract(): 
      yield scrapy.Request(response.urljoin("/" + city), callback = self.parse_area_page) 

    # Step 2 - Get the block links 

    def parse_area_page(self, response): 
     for url in response.xpath('//div[3]/div/div/div/a/@href').extract(): 
      yield scrapy.Request(response.urljoin(url), callback=self.parse_unitpage) 

    # Step 3 Get the room links 

    def parse_unitpage(self, response): 
     for url in response.xpath('//*[@id="subnav"]/div/div[2]/ul/li[5]/a/@href').extract(): 
      yield scrapy.Request(response.urljoin(final), callback=self.parse_final) 

    # Step 4 - Scrape the data 

    def parse_final(self, response): 
     pass 

나는 this answer에 따라 Crawlspider로 변경 시도했다, 그러나 그 도움이 될 것 같지 않았다.

나는 현재 거미를 디버깅하는 방법을 찾고 있지만, 그걸로 어려움을 겪고 있으므로 여기서도 의견을 얻는 것이 도움이 될 것이라고 생각했습니다.

+0

output.csv에서 최종 URL을 저장합니다. 아마도 문제를 찾는 데 도움이 될 것입니다. 'url을 올바르게 작성했는지 확인하기 위해'response.urljoin()'을 표시 할 수도 있습니다. – furas

+0

이 변수가 없다면 왜'parse_unitpage'의'response.urljoin (final)'에'final'을 사용합니까? 오류 메시지가 표시되지 않습니까? – furas

+0

'parse()'의'xpath()'는 결과를주지 않습니다. 'div/div/div /'대신 클래스 이름을 사용할 수 있습니까? – furas

답변

2

당신은 '//*[@id="top"]/div[1]/div/div[1]/div/ul/li/a/text()'

text()()을 잊어하지만 그 대신 text()의 나는 URL을 얻을 @href를 사용합니다. / 건너 뛰고 /uk-student-accommodation이 있기 때문에 가입 urljoin('/' + city)

잘못된 URL을 생성 - 당신이 사용할 필요가 urljoin(city)이 문제가 allowed_domains로했다

- 그것은 대부분의 URL을 차단.


예제. 어쩌면 '기능을 실행하지 않는 한 볼`인쇄()를 사용하면 프로젝트없이 실행할 수 있으며

import scrapy 


class CollegiateSpider(scrapy.Spider): 

    name = 'Collegiate' 

    allowed_domains = ['collegiate-ac.com'] 

    start_urls = ['https://collegiate-ac.com/uk-student-accommodation/'] 

    # Step 1 - Get the area links 

    def parse(self, response): 
     for url in response.xpath('//*[@id="top"]/div[1]/div/div[1]/div/ul/li/a/@href').extract(): 
      url = response.urljoin(url) 
      #print('>>>', url) 
      yield scrapy.Request(url, callback=self.parse_area_page) 

    # Step 2 - Get the block links 

    def parse_area_page(self, response): 
     for url in response.xpath('//div[3]/div/div/div/a/@href').extract(): 
      url = response.urljoin(url) 
      yield scrapy.Request(response.urljoin(url), callback=self.parse_unitpage) 

    # Step 3 Get the room links 

    def parse_unitpage(self, response): 
     for url in response.xpath('//*[@id="subnav"]/div/div[2]/ul/li[5]/a/@href').extract(): 
      url = response.urljoin(url) 
      yield scrapy.Request(url, callback=self.parse_final) 

    # Step 4 - Scrape the data 

    def parse_final(self, response): 
     # show some information for test 
     print('>>> parse_final:', response.url) 
     # send url as item so it can save it in file 
     yield {'final_url': response.url} 

# --- run it without project --- 

import scrapy.crawler 

c = scrapy.crawler.CrawlerProcess({ 
    "FEED_FORMAT": 'csv', 
    "FEED_URI": 'output.csv' 
}) 
c.crawl(CollegiateSpider) 
c.start() 
+0

@furas에 감사드립니다 - 정말 고마워요. 특히 프로젝트없이 실행하는 방법의 마지막 예! – Maverick