【发布时间】:2018-05-21 11:46:51
【问题描述】:
我有一个这样的html:
<tr>
<td>
<b>
<a href=".././statistics/power" title="Exponent of the power-law degree distribution">Power law exponent (estimated) with d<sub>min</sub></a>
</b>
</td>
<td>2.1310 (d<sub>min</sub> = 49)
</td>
</tr>
此外,我有许多其他 html 与此几乎相同,但底部第三行的数字不同。 我想在这个 html 中抓取这些数字,例如 2.1310,但不知道该怎么做。
这是我的代码:
def getLinks(Url):
html=urlopen(Url)
s = '<tr><td><b><a href=".././statistics/power" title' \
'="Exponent of the power-law degree distibution">Power law exponent (estimated) with ' \
'd<sub>min</sub></a></b></td><td>2.1310(d<sub>min</sub> = 49) </td></tr>'
soup = BeautifulSoup(s, 'html.parser')
print(soup.find_all('td')[1].contents[0][:-2])
我可以使用此代码获得 2.1310。
但是当数字变了,面对其他html时不知道怎么定义一个unified's'。类似的html太多了,我在编码时无法复制每个人。
【问题讨论】:
-
你可以使用正则表达式来捕获所有的十进制值
标签: python beautifulsoup web-crawler