2017-05-13 18 views
1

私のコードでは、102の代わりに44のリンクデータしか与えられません。誰かがそれを抽出する理由を私に言うことができますか?私はあなたの助けに感謝します。ウェブサイトからすべてのデータをスクラップする方法は?

import scrapy 
class ProjectItem(scrapy.Item): 
    title = scrapy.Field() 
    owned = scrapy.Field() 
    Revenue2014 = scrapy.Field() 
    Revenue2015 = scrapy.Field() 
    Website = scrapy.Field() 
    Rank = scrapy.Field() 
    Employees = scrapy.Field() 
    headquarters = scrapy.Field() 
    FoundedYear = scrapy.Field() 

クラスProjectSpider(scrapy.Spider):

name = "cin100" 
allowed_domains = ['cincinnati.com'] 
start_urls = ['http://www.cincinnati.com/story/money/2016/11/26/see-which-companies-16-deloitte-100/94441104/'] 

def parse(self, response): 

    # get selector for all 100 companies 
    sel_companies = response.xpath('//p[contains(.,"click or tap here.")]/following-sibling::p/a') 

    # create request for every single company detail page from href 
    for sel_companie in sel_companies: 
     href = sel_companie.xpath('./@href').extract_first() 
     url = response.urljoin(href) 
     request = scrapy.Request(url, callback=self.parse_company_detail) 
     yield request 

def parse_company_detail(self, response):   

    # On detail page create item 
    item = ProjectItem() 
    # get detail information with specific XPath statements 
    # e.g. title is the first paragraph 
    item['title'] = response.xpath('//div[@role="main"]/p[1]//text()').extract_first().rsplit('-')[1] 
    # e.g. family owned has a label we can select 
    item['owned'] = response.xpath('//div[@role="main"]/p[contains(.,"Family owned")]/text()').extract_first() 
item['Revenue2014'] ='$'+response.xpath('//div[@role="main"]/p[contains(.,"2014")]/text()').extract_first().rsplit('$')[1] 
item['Revenue2015'] ='$'+response.xpath('//div[@role="main"]/p[contains(.,"$")]/text()').extract_first().rsplit('$')[1] 
    item['Website'] = response.xpath('//div[@role="main"]/p/a[contains(.,"www.")]/@href').extract_first() 
item['Rank'] = response.xpath('//div[@role="main"]/p[contains(.,"rank")]/text()').extract_first() 
item['Employees'] = response.xpath('//div[@role="main"]/p[contains(.,"Employ")]/text()').extract_first() 
item['headquarters'] = response.xpath('//div[@role="main"]/p[10]//text()').extract() 
item['FoundedYear'] = response.xpath('//div[@role="main"]/p[contains(.,"founded")]/text()').extract() 
    # Finally: yield the item 
    yield item 

答えて

1

で標識したような非会社のURLを削除するには、URLをフィルタリングする必要がある場合があります

以下に示すように要求彼らはリダイレクトされます:

DEBUG: Redirecting (302) to <GET http://www.cincinnati.com/get-access/?return=http%3A%2F%2Fwww.cincinnati.com%2Fstory%2Fmoney%2F2016%2F11%2F27%2Ffrischs-restaurants%2F94430718%2F> from <GET http://www.cincinnati.com/story/money/2016/11/27/frischs-restaurants/94430718/> 

要求されますページは言う:私たちは、あなたの無料アクセスを享受している願っています。

匿名ユーザーへのアクセスが制限されているようです。データに完全にアクセスするには、おそらくサービスに登録する必要があります。

1

あなたのXPathを持ついくつかの潜在的な問題があります。

  1. が、それはXPathには、テキストを見えるようにするために、通常は悪い考えですそれはページ上にあります。テキストは1分ごとに変わることがあります。レイアウトとhtml構造ははるかに長く存続します。

  2. 'following-siblings'を使用することは、ウェブサイトのわずかな変更に対して非常に脆弱な最後の手段であるxpath機能です。私が代わりにやっているだろうか

# iterate all paragraphs within the article: 
for para in response.xpath("//*[@itemprop='articleBody']/p"): 
    url = para.xpath("./a/@href").extract() 
    # ... etc 

len(response.xpath("//*[@itemprop='articleBody']/p"))は私のやり方によって期待される102を与えます。あなたは上の近くにあなたが数十後に開始することを見つけるscrapyの出力でみる「をクリックするか、ここをタップ」

関連する問題