从<th>标签获取价值

sma*_*str 2 html python parsing

我怎样才能从中读取值

<th class="class_name"> Sample Text </th>
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我使用python从上面的HTML代码中获取字符串"Sample Text".

谢谢.

luc*_*luc 6

你可以使用BeautifulSoup这是我最喜欢的lib来解析html.

from BeautifulSoup import BeautifulSoup
html = '<th class="class_name"> Sample Text </th>'
soup = BeautifulSoup(html)
print soup.th.text
Run Code Online (Sandbox Code Playgroud)