Python 3-从beautifulSoup中的标签获取文本
我正在使用beautifulSoup从网站提取数据。每当您重新加载页面时,该网站上的文本都会更改,因此基本上,我希望能够将重点放在类名上作为静态变量,因为文本是动态的。
import requests
from bs4 import BeautifulSoup
url = 'xxxxxxxxxxx'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
class2 = soup.find_all(True, class_="template_title")
print (class2)
它打印出
<td align="left" class="template_title" height="50" valign="bottom"
width="535"><div style="padding-bottom:9px;">4</div></td>
当页面重新加载,我仍然有重点的领域,但我不知道如何只打印文本(在这种情况下是:4)
一旦解决了这个问题,我还有另一个问题:如果该类包含多个标签,是否有办法获取更多静态数据,以确保它只打印我在搜索的文本,而不是其他文本?(我有课,但是我也可以使用height
=“ 50” valign =“ bottom” width =“ 535”吗?)