在BS的評論對象的數據,以及評論的對象僅僅是一種特殊類型的NavigableString的,你需要做的是:
查找包含該信息的刺痛
使用BeautifulSoup轉換字符串對象BS
提取數據從BS對象
代碼:
import re
table_string = soup.find(string=re.compile('div_team_misc'))
這將返回一個包含表的HTML代碼的刺痛。
table = BeautifulSoup(table_string, 'lxml')
使用的刺痛從對象
for tr in table.find_all('tr', class_=False):
s = [td.string for td in tr('td')]
print(s)
出構建BS對象,並提取數據:
['17', '13', '2.17', '-0.51', '1.66', '106.9', '104.7', '96.5', '.300', '.319', '.493', '10.9', '20.5', '.228', '.501', '11.6', '79.6', '.148', 'Spectrum Center', '269,471']
['10', '9', '8', '24', '10', '17', '5', '15', '4', '11', '22', '1', '27', '5', '12', '28', '3', '1', None, '15']
更多評論:
markup = "<b><!--Hey, buddy. Want to buy a used parser?--></b>"
soup = BeautifulSoup(markup)
comment = soup.b.string
Comment對象只是一個特殊類型的NavigableString,BS會從中提取字符串,我們不需要更改或替換任何html。
comment
# u'Hey, buddy. Want to buy a used parser'
在此基礎上,我們可以用它代替re
純BS提取評論
table_string = soup.find(id="all_team_misc").contents[-2]
如果你有什麼發現所有的表字符串,你可以這樣做:
from bs4 import Commnet
tables = soup.find_all(string=lambda text:isinstance(text,Comment) and str(text).startswith(' \n'))
請將該圖片直接包含在您的問題中,而不是隨時可能會被破解的鏈接。 – ettanany
此頁面包含HTML中的所有數據,但隱藏爲註釋並使用JavaScript顯示。但是你可以用'BeautifuSoup'來找到這個註釋,刪除'<! - '和' - >'並用'BeautifuSoup'使用結果來獲取數據。我認爲這個問題在之前的一些問題中得到了解決。 – furas