2016-10-21 2 views
0

일부 데이터를 스크래핑하기 위해 치료를 사용하고 있으며 최근에 스크래핑을 시작한 페이지 중 하나는 정상적인 것보다 2 개의 테이블이 있습니다. 테이블을 따로 따로 긁어 내고 ID에 ID를 부여하고 싶습니다. 나는 다양한 경로 (아래 참조)를 사용하여 시도해 보았고 함께 긁힌 두 테이블, 항목의 빈 사전 또는 경로를 찾지 않는 병사로 끝납니다. 이 사이트는 여기에 있습니다 :페이지에서 두 개의 테이블로 올바른 데이터를 가져 오지 못합니다.

http://www.faa.gov/data_research/commercial_space_data/licenses/

내가 돌아올 수없는 값으로 시도 크롤러 response.xpath()은 다음과 같습니다

(//table)[1]/tbody 

그리고 경우 : 빈 Scrapy 항목을 반환

//*[@id="DataTables_Table_0"] 
    //*[@id="DataTables_Table_1"] 
    /html/body/div[2]/div/div[2]/div[1]/table 
    /html/body/div[2]/div/div[2]/div[2]/table 

XPath는 //tbody//tr을 예상대로 사용하면 결국 두 테이블을 모두 포함하는 목록이됩니다.

내 거미 코드 :

from scrapy.spiders import Spider 
import items as spi  

class ActiveLaunchLicenseSpider(Spider): 
    name = "faa_actlnchlic" 
    allowed_domains = ['faa.gov'] 
    start_urls = ['http://www.faa.gov/data_research/commercial_space_data/licenses/'] 

    def parse(self, response): 
     licenses = response.xpath('//tbody') 
     for license in licenses: 
      license_item = spi.ActiveLaunchLicenseScraperItem() 
      license_item['license'] = license.xpath('//tr/td[1]/a').extract() 
      license_item['company'] = license.xpath('//tr/td[2]').extract() 
      license_item['vehicle'] = license.xpath('//tr/td[3]').extract() 
      license_item['location'] = license.xpath('//tr/td[4]').extract() 
      license_item['expiration'] = license.xpath('//tr/td[5]/span').extract() 
      yield license_item 

누군가가 나를 중 하나를 이해하는 데 도움이 수 있습니까 ID를 기반으로 경로가 한 번에 하나 개의 테이블을 선택하는 좋은 방법을 (를 식별 할 수 방화범 사용) 잘못된 얼마나?

+0

흠 아마도 내가 조금이를 시도 할 것이다 스크랩 도움이 될 수 있습니다. 이것과 관련된 선택자 정보를 검색 할 때 그 정보가 누락되지 않았 음을 알려 주시면 감사하겠습니다. – Alexander

답변

0

다음 코드는 데이터

import scrapy 
from faa_gov.items import FaaGovItem 
class faa_gov(scrapy.Spider): 
    name = "faa_actlnchlic" 
    allowed_domains = ['faa.gov'] 
    start_urls = ['http://www.faa.gov/data_research/commercial_space_data/licenses/'] 

    def parse(self, response): 
     licenses = response.xpath("//caption[text()='Active Launch Licenses']/following-sibling::tbody[1]/tr") 
     for license in licenses: 
      license_item = FaaGovItem() 
      license_item['license'] = license.xpath('.//td[1]/a').extract() 
      license_item['company'] = license.xpath('.//td[2]').extract() 
      license_item['vehicle'] = license.xpath('.//td[3]').extract() 
      license_item['location'] = license.xpath('.//td[4]').extract() 
      license_item['expiration'] = license.xpath('.//td[5]/span').extract() 
      yield license_item 
+0

위 코드는 코드와 함께 파이프 라인의 행에 저장되어 있기 때문에 조금 수정했습니다. 경로를'licenses = response.xpath ("caption [text() = '활성 실행 라이센스]/following-sibling :: tbody [1]')'로 변경하고 선택기가 tr 레벨에서 시작하도록 경로를 업데이트했습니다. license_item [ 'license'] = license.xpath ('.// tr/td [1]/a'). extract()'는 테이블의 모든 행을 나에게주었습니다. – Alexander

관련 문제