簡體   English   中英

如果其他HTML位於標記中,如何從div標記中提取python中的文本?

[英]How to extract text in python from div tag if other html is within the tag?

我正在嘗試提取參考。 HTML中的scrapy的ID:

<div class="col" itemprop="description">
  <p>text Ref.&nbsp;<span>220.20.34.20.53.001</span></p>
  <p>more text</p>
</div>

span和p標簽並不總是存在。

使用xpath選擇器:

text = ' '.join(response.xpath('//div[@itemprop="description"]/p/text()').extract()).replace(u'\xa0', u' ')
try: 
     ref_id = re.findall(r"Ref\.? ?((?:[A-Z\d\.]+)|(?:[\d.]+))", text)[0].strip()

在這種情況下,僅返回一個空字符串,因為標記內有HTML。

現在嘗試使用CSS選擇器提取文本以使用remove_tags:

>>> ''.join([remove_tags(w).strip()for w in response.css('div[itemprop="description"]::text').extract()]) 

由於我無法以某種方式抓取該項目,因此返回空結果。

無論div中是否包含html <p>標記,如何提取ref_id。 我第一次嘗試使用xpath時,某些爬網項目沒有<p>標記,也沒有<span>

嘗試從最后一個表達式中刪除::text

''.join([remove_tags(w).strip() for w in response.css('div[itemprop=description]').extract()]) 

但是,如果您只需要從html中提取220.20.34.20.53.001 ,為什么不使用response.css('div[itemprop=description] p span::text').extract()

甚至是response.css('div[itemprop=description]').re(r'([\\.\\d]+)')

您無需使用remove_tags因為您可以使用選擇器直接獲取text

sel.css('div[itemprop=description] ::text')

這將從div標記中獲取所有帶有itemprop="description"內部文本,隨后您可以使用正則表達式提取信息:

sel.css('div[itemprop=description] ::text').re_first('(?:\d+.)+\d+')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM