相关疑难解决方法(0)

使用BeautifulSoup删除标记但保留其内容

目前我的代码执行如下操作:

soup = BeautifulSoup(value)

for tag in soup.findAll(True):
    if tag.name not in VALID_TAGS:
        tag.extract()
soup.renderContents()
Run Code Online (Sandbox Code Playgroud)

除了我不想丢弃无效标签内的内容.如何在删除标签但在调用soup.renderContents()时保留内容?

python beautifulsoup

48
推荐指数
6
解决办法
5万
查看次数

获取所有儿童文本的xpath

有没有办法在ul标签中获取所有子节点值.

输入:

<ul>
    <li class="type">Industry</li> 

    <li><a href="/store/Browse/?N=355+361+4294855087">Automotive</a></li>                            

    <li><a href="/store/Browse/?N=355+361+4294855065">Parts </a></li>                                

    <li>Tires</li>                  
</ul>
Run Code Online (Sandbox Code Playgroud)

产出:工业,汽车,零件,轮胎.

xpath

21
推荐指数
2
解决办法
2万
查看次数

Python NLTK莎士比亚语料库

我正在尝试从莎士比亚的NLTK语料库中导入句子-在帮助网站之后-但是我在访问句子时遇到困难(以便训练word2vec模型):

from nltk.corpus import shakespeare #XMLCorpusreader
shakespeare.fileids()
['a_and_c.xml', 'dream.xml', 'hamlet.xml', 'j_caesar.xml', ...]

play = shakespeare.xml('dream.xml') #ElementTree object
print(play)
<Element 'PLAY' at ...>

for i in range(9):
    print('%s: %s' % (play[i].tag, play[i].text))
Run Code Online (Sandbox Code Playgroud)

返回以下内容:

TITLE: A Midsummer Night's Dream
PERSONAE: 

SCNDESCR: SCENE  Athens, and a wood near it.
PLAYSUBT: A MIDSUMMER NIGHT'S DREAM
ACT: None
ACT: None
ACT: None
ACT: None
ACT: None
Run Code Online (Sandbox Code Playgroud)

为什么所有行为都没有?

此处定义的方法(http://www.nltk.org/howto/corpus.html#data-access-methods)(.sents(),tag_sents(),chunked_sents(),parsed_sents())似乎都无效当应用于莎士比亚XMLCorpusReader时

我想了解:
1 /如何获得句子

2 /如何知道如何在ElementTree对象中寻找它们

python nlp nltk

5
推荐指数
1
解决办法
820
查看次数

标签 统计

python ×2

beautifulsoup ×1

nlp ×1

nltk ×1

xpath ×1