首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在美丽汤中复制标签后的每一件东西

如何在美丽汤中复制标签后的每一件东西
EN

Stack Overflow用户
提问于 2017-10-29 06:06:54
回答 1查看 290关注 0票数 0

在做作业时,我有"doc.html“文件和数据:

代码语言:javascript
复制
<span class="descriptor">Title:</span> Automated Scalable Bayesian Inference via Hilbert Coresets
<span class="descriptor">Title:</span> PASS-GLM: polynomial approximate sufficient statistics for scalable  Bayesian GLM inference
<span class="descriptor">Title:</span> Covariances, Robustness, and Variational Bayes
<span class="descriptor">Title:</span> Edge-exchangeable graphs and sparsity (NIPS 2016)
<span class="descriptor">Title:</span> Fast Measurements of Robustness to Changing Priors in Variational Bayes
<span class="descriptor">Title:</span> Boosting Variational Inference

对于每一行,我都试图在</span>之后得到任何东西--所以预期的输出应该是:

代码语言:javascript
复制
Automated Scalable Bayesian Inference via Hilbert Coresets
PASS-GLM: polynomial approximate sufficient statistics for scalable  Bayesian GLM inference
Covariances, Robustness, and Variational Bayes
Edge-exchangeable graphs and sparsity (NIPS 2016)
Fast Measurements of Robustness to Changing Priors in Variational Bayes
Boosting Variational Inference

我尝试了下面的代码(不起作用)。

代码语言:javascript
复制
from bs4 import BeautifulSoup

with open("doc.html") as fp:
    soup = BeautifulSoup(fp, 'html.parser')
    for line in soup.find_all('span'):
        print line.get_text()

丢失的那块是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-29 06:13:14

您需要span元素的nextSibling,而不是span内部的text

注意:使用条带()删除尾随的换行符。

代码语言:javascript
复制
>>> with open("doc.html") as fp:
...     soup = BeautifulSoup(fp, 'html.parser')
...     for line in soup.find_all('span'):
...         print line.nextSibling.strip()
... 
Automated Scalable Bayesian Inference via Hilbert Coresets
PASS-GLM: polynomial approximate sufficient statistics for scalable  Bayesian GLM inference
Covariances, Robustness, and Variational Bayes
Edge-exchangeable graphs and sparsity (NIPS 2016)
Fast Measurements of Robustness to Changing Priors in Variational Bayes
Boosting Variational Inference
>>> 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46997325

复制
相关文章

相似问题

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