首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >BeautifulSoup在标记内找不到文本

BeautifulSoup在标记内找不到文本
EN

Stack Overflow用户
提问于 2018-05-31 16:53:14
回答 1查看 26关注 0票数 0

它看起来很简单,但我没有发现错误,我现在已经在上面花了几个小时:

from bs4 import BeautifulSoup 

txt = "<HTML><BODY><TABLE><TR><TD><P>Net sales</P></TD></TR></TABLE></BODY></HTML>"
print(txt)
bs = BeautifulSoup(txt, 'html.parser')
q = bs.find(tag='p', text='Net sales')
# table = q.find_parent('table')
print("q = ", q)

这导致(在Jupyter中)

<HTML><BODY><TABLE><TR><TD><P>Net sales</P></TD></TR></TABLE></BODY></HTML>
q =  None

为什么找不到“净销售额”这一文本?

最后,我想要获取table-tag中的文本,我只是在上面的代码中注释了这一行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-31 17:03:50

尝试不使用'tag‘参数

Ex:

from bs4 import BeautifulSoup

txt = "<HTML><BODY><TABLE><TR><TD><P>Net sales</P></TD></TR></TABLE></BODY></HTML>"
bs = BeautifulSoup(txt, 'html.parser')
print bs
q = bs.find('p', text='Net sales')
# table = q.find_parent('table')
print("q = ", q)

输出:

('q = ', <p>Net sales</p>)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50620302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档