0
現在、私のスクラップコードに関する問題があります。私はウェブサイトを廃止し、データを取得しようとしています。場合によっては、一部のページでは、このデータが存在しない場合があります。次に、予期したとおり、よくわかっているインデックスエラーが発生します。 しかし、それに付随するすべてのレコードは出力ファイルに入れられません。それは面倒です。索引エラー時にスクリーニングが出力されない場合
どのように解決策を見つけることができますか?私はresponse.xpath( "whatever_data")= ""を使って試しました。.. else。
私はtryメソッドを試してみましたが、私は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(),
}
ベスト、フィードバックのための
あなたのコードをフォーマットするためにSOエディタに貼り付けますそれを強調表示し、 '{}'ボタンをクリックしてコードブロックに書式設定すると、インデントがすべての場所に表示されます。 –
'indexerror'は空リストから' [0] 'を取得することを意味します - ' xpath() 'が空文字列を返さないため、' response=path(...): '' '== 'しかし、空のリスト。 – furas