2017-12-06 19 views
1

が含まれている場合、私は、次のWebページがあります。scrapy:ノードが子ノード

<dd> 
<span class="signpost-site" data-site="news">News 
</span> 
<span class="signpost-section">Europe 
</span> 
</dd> 

:各記事の https://www.bbc.co.uk/search?q=Juice&sa_f=search-product&filter=news&suggid=

を、私は私がからのテキストをこすりしたい、次のHTMLセクションを持っていますこの場合、私は時々

<span class="signpost-section"> 
"ヨーロッパ" をしたいです意図はCSVファイルを作成し、それぞれの記事は、右のインデックス番号で適切なタグを持っていることを確認することです

「」

が不足しているし、代わりに私が欲しいこの場合

<dd> 
<span class="signpost-site" data-site="news">News 
</span> 
</dd> 

があります。

現在、私のコードは、既存のタグを取得し

response.xpath('//footer//dd/span[@class="signpost-section"]/text()').extract() 

です。私は、理想的に私は、私はちょうど、指定し.extract_first()を使用することになり

if <span class="signpost-section"> (exists in) response.xpath('//footer//dd/span[@class="signpost-site"]) 
then 
response.xpath('//footer//dd/span[@class="signpost-section"]/text()').extract() 
else "" 

答えて

1

の線に沿って何かをしたい

response.xpath('//footer//dd/span[@class="signpost-site"]) 

内に存在する

<span class="signpost-section"> 

かどうかを確認する方法についてわかりませんよ デフォルト値(一致しない場合に使用):

response.xpath('//footer//dd/span[@class="signpost-section"]/text()').extract_first(default='') 
+0

私は各見出しのすべてのタグが必要ですか? extract_first()は最初の記事を取得します –