2017-04-26 9 views
1

の内側にタグのテキストを取得する方法を私はこのようなHTMLの段落を持っています。強いタグの部分でさえ。 私もCSSのように第一子を試してみましたが、この時間は何も返されません。:Scrapyは:別のタグ

for text in response.css("div.entry-content"): 
     yield { 
      "parag": text.css("p::text").extract(), 
     } 

を、私は以下のコードを試してみましたが、私は唯一の「こんにちは」を取得:

"parag": text.css("p:strong::text").extract() 

編集:代わりの強いですそれは別のタグかもしれません。だから、目標は、最初の子テキスト

+0

CSSタグがここで助けにはなりません。) –

答えて

3

を取得することですここで働い例です:

>>> from scrapy.http import HtmlResponse 
>>> response = HtmlResponse(url="Test HTML String", body="<p>Hello <strong>I'm G </strong> <b>I write code</b></p>") 

# First child 
>>> ' '.join(t.strip() for i, t in enumerate(response.css('p ::text').extract()) if i< 2).strip() 
u"Hello I'm G" 

# All child 
>>> ' '.join(t.strip() for t in response.css('p ::text').extract()).strip() 
u"Hello I'm G I write code"