本文整理汇总了Python中bs4.Tag.new_tag方法的典型用法代码示例。如果您正苦于以下问题:Python Tag.new_tag方法的具体用法?Python Tag.new_tag怎么用?Python Tag.new_tag使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bs4.Tag
的用法示例。
在下文中一共展示了Tag.new_tag方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: figure_hyperlink
# 需要导入模块: from bs4 import Tag [as 别名]
# 或者: from bs4.Tag import new_tag [as 别名]
def figure_hyperlink(soup: Tag) -> Tag:
"""
Make figures out of images in links.
By default, pandoc transforms
[![foo](bar)](baz)
![foo](bar)
into (minus whitespace)
<p><a href="baz"><img alt="bar" /></p>
<figure>
<img alt="foo" src="bar" />
<figcaption>foo</figcaption>
</figure>
We want the first to also be a figure.
"""
# BeautifulSoup 4, as of this writing, doesn't support these
# pseudo-classes.
#for img in soup.select('p > a:only-child > img:only-child[alt]'):
for img in soup.select('p > a > img[alt]'):
# <p><a><img alt='not blank' /></a></p>
a = img.parent
p = a.parent
if len(a.contents) != 1 or \
len(p.contents) != 1:
continue
figure = soup.new_tag('figure')
figcaption = soup.new_tag('figcaption')
figcaption.append(img.attrs['alt'])
a.append(figcaption)
figure.append(a)
p.replaceWith(figure)
return soup