簡體   English   中英

Python-使用BS4從此Html標簽提取數據,而不是獲取None

[英]Python - Extracting data from this Html tag using BS4, instead of getting None

這是我的代碼:

html = '''
<td class="ClassName class" width="60%">Data I want to extract<span lang=EN- 
UK style="font-size:12pt;font-family:'arial'"></span></td>
'''


soup = BeautifulSoup(html, 'html.parser')

print(soup.select_one('td').string)

它返回None。 我認為這與空的span標簽有關。 我認為它進入了span標簽,並返回了那些內容? 所以我要么刪除該span標簽,要么在找到“我要提取的數據”后立即停止,或者告訴它忽略空標簽

如果'td'中沒有空標簽,則它實際上可以工作。

是否有一種方法通常可以忽略空標簽並向后退一步? 而不是忽略此特定的span標簽?

抱歉,這太基礎了,但是我花了很多時間搜索。

使用.text屬性,而不是.string

html = '''
<td class="ClassName class" width="60%">Data I want to extract<span lang=EN-
UK style="font-size:12pt;font-family:'arial'"></span></td>
'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'html.parser')

print(soup.select_one('td').text)

輸出:

我要提取的數據

使用.text

>>> soup.find('td').text
u'Data I want to extract'

暫無
暫無

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

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