使用BeautifulGroup从类中提取子类

2024-09-28 19:07:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python3.6.3处理Goodreads页面中的以下HTML片段:

<div class="quoteText">
      “Don't cry because it's over, smile because it happened.”
  <br/>  ―
    <a class="authorOrTitle" href="/author/show/61105.Dr_Seuss">Dr. Seuss</a>
</div>, <div class="quoteText">

我使用beauthoulsoup来获取HTML并仅隔离上面片段中的“quoteText”类。现在,我想将引号和作者名保存为单独的字符串。我可以用

^{pr2}$

我不知道该怎么做。我想我需要一种从输出中删除子类的方法,并尝试使用extract方法。在

quote.extract(class_="authorOrTitle")

但我有一个错误,说extract得到了一个意外的关键字参数'class' 我想做的事还有别的办法吗?在

这是我第一次在这里发帖,所以如果帖子不符合特殊性/格式/其他标准,我深表歉意。在


Tags: 方法divhtmlextractit页面classgoodreads
1条回答
网友
1楼 · 发布于 2024-09-28 19:07:32

PageElement.extract() removes a tag or string from the tree. It returns the tag or string that was extracted

from bs4 import BeautifulSoup
a='''<div class="quoteText">
      “Don't cry because it's over, smile because it happened.”
  <br/>  -
    <a class="authorOrTitle" href="/author/show/61105.Dr_Seuss">Dr. Seuss</a>
</div>, <div class="quoteText">'''
s=BeautifulSoup(a,'lxml')
s.find(class_="authorOrTitle").extract()
print(s.text)

相关问题 更多 >