python - 如何使用 BeautifulSoup 从 <td> 获取文本?

标签 python html beautifulsoup

我尝试从如下所示的 html 中获取文本:

</td><td align='center'>&nbsp;</td><td align='right'>0.1200</td><td align='left'><img

我有兴趣获取数字“0.1200”。

我的代码是这样的:

`url = "http://...."
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "lxml")
for link in soup.findAll('td', {'align': 'right'}):
    for row in link.find_next_sibling("td"):
        print(row)

我收到一条错误消息“TypeError:'NoneType'对象不可迭代”。 关于解决该问题有什么建议吗?

最佳答案

你能尝试一下吗? (如果您不提供网址,我们将很难为您提供帮助)

代码:

soup = BeautifulSoup(plain_text, "lxml")
for link in soup.findAll('td', {'align': 'right'}):
    print(link.text)

关于python - 如何使用 BeautifulSoup 从 <td> 获取文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47560004/

相关文章:

python - 将元素添加到空列表会产生意外行为

python - numpy object_ 数组中最长的字符串

html - Google Analytics(分析)中的UserID或Fix用户跟踪

html - 垂直对齐 float 图像右侧的文本,图像大小可变,响应式

html - 如何通过CSS更改图像覆盖整个div

python - BeautifulSoup 找不到具有特定类的 div

python - 使用递归从嵌套列表中获取绝对值

python - 我怎样才能 "condense"这个代码?

python - 如何从 html 源代码中具有相同属性集和相同层次结构的 2 个元素中抓取单个元素(使用 python 的漂亮汤)

python - 在 BeautifulSoup 中扩展 CSS 选择器