0

現在、スクレイピー コードに問題があります。ウェブサイトをスクレイピングして、データを取得しようとしています。一部のページでは、このデータが存在しない場合があり、予想どおり、非常によく知られている indexerror が発生します。しかし、それに添付されたすべてのレコードは、どちらも出力ファイルに入れられません: それは面倒です.

どうすればその解決策を見つけることができますか? if response.xpath("whatever_data")="":.. else.. で試しました

私はtry methodeを作ろうとしました.indexerrorを除外しようとしました..何もうまくいきませんでした.

何か案が?

これが私のコードです:

class QuotesSpider(scrapy.Spider):
name = "quotes"

   start_urls = ['http://www.verif.com/recherche/?search=v/1/ca/h_siren=&h_code_ape=2060Z']

def parse(self, response):
    for lien_fiche in response.css('a::attr(href)').re(r'\/societe\/.+'):

        yield scrapy.Request(response.urljoin(lien_fiche), callback=self.parse_fiche)
    next_page = response.css('a.btn-page.btn-next::attr(onclick)').re(r'/recherche.+2060Z')[0]
    if next_page is not None:

        next_page = response.urljoin(next_page)
        yield scrapy.Request(next_page, callback=self.parse)

def parse_fiche(self, response):

        code_ape = "2060Z"

        yield {
            'nom': response.xpath('//td[@class="tdhead"][text()="Raison sociale "]/following-sibling::td/text()').extract_first(),
            'CA 2015' : response.xpath('//td[@class="tdhead"][text()="Chiffre d\'affaires 2015 "]/following-sibling::td/a/text()').re(r'\n\s+([0-9€ ]+)'),
            'Capital social' : response.xpath('//td[@class="tdhead"][text()="Capital Social "]/following-sibling::td/text()').extract_first(),
            'SIRET': response.xpath('//td[@class="tdhead"][text()="SIRET "]/following-sibling::td/text()').extract_first(),
            'code APE': code_ape,
            'effectif': response.css('script').re(r'=(effectif.+);ca'),
            'dirigeant':
                response.xpath('//table[@class="table table-default dirigeants"]/tr/td[@class="tdhead"]/text()')[
                    0].extract() + " " + response.xpath(
                    '//table[@class="table table-default dirigeants"]/tr/td[@class="tdhead"]/following-sibling::td/a/text()')[
                    0].extract(),

        }

一番、

4

1 に答える 1