Python 从HTML读取元素-

Python 从HTML读取元素-,python,html,parsing,python-3.x,html-table,Python,Html,Parsing,Python 3.x,Html Table,我有以下HTML: <tr style='background:#DDDDDD;'> <td><b>ASD</b></td> <td colspan='3'>1231</td> </tr> 此元素没有在页面上重复,因此它是唯一的。我想将单元格的内容1231放入某个变量中。我试过使用HTML.parser,但不起作用 from BeautifulSoup import Beautifu

我有以下HTML:

<tr style='background:#DDDDDD;'>
    <td><b>ASD</b></td>
    <td colspan='3'>1231</td>
</tr>

此元素没有在页面上重复,因此它是唯一的。我想将单元格的内容1231放入某个变量中。我试过使用HTML.parser,但不起作用

from BeautifulSoup import BeautifulSoup

soup = BeautifulSoup(html) ## feed your html page to beautifulsoup

pleaseFind = soup.find(text="ASD")

whatINeed = pleaseFind.findNext('td')

print whatINeed.text

您可以使用urllib2,您不必安装任何新的内容,至少对于Windows版本的Python:

例如:

import urllib2
response = urllib2.urlopen('your URL')
html = response.read()
#html is a string containing everything on your page

#this line (it could be a bit cleaner) finds substring "<td colspan='3'>" and
#searches between it's position and the next "</td>"
pos=html.find("<td colspan='3'>")
print html[pos+len("<td colspan='3'>")+1:html.find("</td>", pos))]

你能告诉我们你试过什么吗?汤做的:-谢谢没问题,你可以用它来找到锚、段落、标题或任何你需要的东西。