【问题标题】:Python, extract tags and also get position of wordPython,提取标签并获取单词的位置
【发布时间】:2012-07-22 04:25:56
【问题描述】:

我有一个字符串,

data = 'very <strong class="keyword">Awesome</strong> <strong class="keyword">Book</strong> discount'

我想将列表中的输出作为

ans = ['very','<strong class="keyword">Awesome</strong>','<strong class="keyword">Book</strong>','discount']

所以我可以知道单词的位置以及标签中出现的单词。 我使用 BeautifulSoup 提取单词 in 和单词 with are not in 。但我需要找到位置。 我试过的代码。

from bs4 import BeautifulSoup as BS
data = 'very <strong class="keyword">Awesome</strong> <strong class="keyword">Book</strong>'
soup = BS(data)
to_extract = soup.findAll('strong')
[comment.extract() for comment in to_extract]
soup = str(soup)
notInStrongWords = []
for t in to_extract:
    t_soup = BS('{0}'.format(t))
    t_tag = t_soup.strong
    matchWords.append(t_tag.string)
soup = re.sub("[^A-Za-z0-9\\-\\.\\(\\)\\\\\/\\&': ]+",' ', soup)
soup = re.findall('[(][^)]*[)]|\S+', soup)
InStrongWords = []
InStrongWords = [x for x in soup]

提前致谢。

【问题讨论】:

  • 您问题中的 input -> output 语句似乎没有区分 - 它是所有标签的列表。然而,代码会专门寻找 。这也需要吗?

标签: python regex beautifulsoup


【解决方案1】:

re.finditer(而不是re.findall)为您提供match 对象,您可以获得start()end() 的。

【讨论】:

  • 能否举个例子。
  • 这将为我提供“非常”和“折扣”的位置,因为“强”中的单词已经使用 BeautifulSoup 提取。可能我必须在不使用 BeautifulSoup 的情况下尝试一种全新的方法。
【解决方案2】:

尝试(对于 Python 2.x - Python 3 的 unicode 不同):

from bs4 import BeautifulSoup as BS
data = 'very <strong class="keyword">Awesome</strong> <strong class="keyword">Book</strong>'
soup = BS(data)
pTag = soup.p
list = [ unicode(child) for child in pTag.children ]
print list

返回:

[u'very ', u'<strong class="keyword">Awesome</strong>', u' ', u'<strong class="keyword">Book</strong>']

基本上,遍历子元素并将它们转换回 Unicode 字符串。您可能想要过滤掉空格,但这在技术上存在于您的 HTML 中。

如果您需要检查哪些孩子“强壮”,您可以这样做:

import bs4

data = 'very <strong class="keyword">Awesome</strong> <strong class="keyword">Book</strong>'
soup = bs4.BeautifulSoup(data)

list = [ (child.name if isinstance(child, bs4.Tag) else None, unicode(child)) for child in soup.children ]
print list

它返回一个元组列表,每个元组是(标签的名称或 None 没有标签,HTML):

[(None, u'very '), (u'strong', u'<strong class="keyword">Awesome</strong>'), (None, u' '), (u'strong', u'<strong class="keyword">Book</strong>')]

【讨论】:

  • 运行相同的代码不会在汤变量上进行迭代,因为 和 标记已添加到其中。结果是这样的 [u'

    非常 Awesome Book

    '] ...我使用的是python 2.7。
【解决方案3】:

根据 Andrew Alcok 的回答,谢谢 Ansrew。

让我们说,

data = ['very <strong class="keyword">Awesome</strong> <strong class="keyword">Book</strong>','<strong class="keyword">Awesome</strong> <strong class="keyword">Book</strong> discount']

对于 python 2.x 和 BeautifulSoup 4

from bs4 import BeautifulSoup as BS
for d in data:
    soup = BS(d)
    soupPTag = soup.p
    if soupPTag:
        soupList = [unicode(child) for child in soupPTag.children if child!=" "]
        print soupList
    else:
        soupBodyTag = soup.body
        soupList = [unicode(child) for child in soupBodyTag.children if child!=" "]
        print soupList

这将给出所需的答案。

【讨论】:

    猜你喜欢
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    相关资源
    最近更新 更多