【问题标题】:Can't seem to append text to tag in BeautifulSoup似乎无法在 BeautifulSoup 中将文本附加到标签
【发布时间】:2018-11-21 20:13:47
【问题描述】:

我从网页中抓取文本和 HTML 链接信息,并尝试将其附加到 <p> 标记,但这不起作用。让我举例说明:

data = """
<div class="Answer">
1. BOUNDARIES - EPB &amp; APL&nbsp;<i>(inferior)</i>, EPL&nbsp;<i>(superior).&nbsp;</i><div>2. FLOOR (proximal to distal) - radial styloid =&gt; scaphoid =&gt; trapezium =&gt; 1st MC base.&nbsp;<br /><div>3. CONTENTS - cutaneous branches of radial nerve&nbsp;<i>(on the roof),</i>&nbsp;cephalic vein&nbsp;<i>(begins here),</i>&nbsp;&nbsp;radial artery&nbsp;<i>(on the floor).</i></div></div><div><br /></div><div><img src="paste-27a44c801f0776d91f5f6a16a963bff67f0e8ef3.jpg" /><br /></div><div><b>Image:&nbsp;</b>Case courtesy of Dr Sachintha Hapugoda, &lt;a href="https://radiopaedia.org/"&gt;Radiopaedia.org&lt;/a&gt;. From the case &lt;a href="https://radiopaedia.org/cases/52525"&gt;rID: 52525&lt;/a&gt; [Accessed 15 Nov. 2018].</div>
</div>
"""

soup = BeautifulSoup(data, "html.parser")
image_link = soup.find('div').find('b').next.next
new_tag = soup.new_tag('p', image_link)
print(new_tag.append(image_link))

以上返回None。我该怎么办?

【问题讨论】:

    标签: python beautifulsoup tags appendtext


    【解决方案1】:

    应该是

    new_tag = soup.new_tag('p')
    new_tag.append(image_link)
    print(new_tag)
    

    【讨论】:

    • 他射门……他得分!大声笑你又来了。我们需要建立一个 bs4 discord group 什么的。
    猜你喜欢
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多