python - 我该怎么做才能知道 Beautifulsoup 中家长的类(class)名称?

标签 python beautifulsoup

我的html看起来像这样,我想分别检索“INFO”。

<tr tabindex = "0">
<td data-name = "isu_srt_cd">"INFO"</td>
<td data-name = "com_abbrv">"INFO"</td>
...
<td data-name = "etf_obj">"INFO"</td>
</tr>

到目前为止我所做的如下,

for ana in soup.find_all("td"):
    if ana.parent.name == "tr":
        print(ana.get_text())

差不多完成了,但我想指定父级的“tabindex”为“0”

最佳答案

查找所有<tr>您感兴趣的话就获取所有<td>在它下面。

for tr in soup.find_all("tr", tabindex='0'):
    for ana in tr.find_all('td'):
        print(ana.get_text())

关于python - 我该怎么做才能知道 Beautifulsoup 中家长的类(class)名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49708392/

相关文章:

python - 使用 BeautifulSoup 从表中的单元格中提取值

python - BeautifulSoup:在每个标题后获取所有 <ul> 的所有内容

python - 如何用 Pandas 中的单个句点和问号替换 '..' 和 '?.'? df ['column' ].str.replace 不起作用

python - 用于 Sublime Text 的构建感知 Python 自动完成

python - 在多列上排名并标记 pandas 观察之间的关系

python - 如何创建在偶尔设置了 nonetype 的功能输入中发生 NoneType 错误的解决方法

python - 请求返回 URL https ://www. instagram.com/google 的状态代码 429

c++ - Python 最大化一个核心,而 C++ 没有

python - 如何更改 Plotly 保存图像的图像大小?

python - beautifulsoup 只提取前 10 个元素