Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法在BeautifulSoup中添加文本和指向新标记的链接_Python_Beautifulsoup - Fatal编程技术网

Python 无法在BeautifulSoup中添加文本和指向新标记的链接

Python 无法在BeautifulSoup中添加文本和指向新标记的链接,python,beautifulsoup,Python,Beautifulsoup,我删除了以下HTML以获取链接信息,创建了一个新标记,将链接添加到新标记,然后尝试将该标记附加到另一个文档,但丢失了所有HTML格式: data = """ <div class="Answer"> 1. BOUNDARIES - EPB &amp; APL&nbsp;<i>(inferior)</i>, EPL&nbsp;<i>(superior).&nbsp;</i><div>2. FLO

我删除了以下HTML以获取链接信息,创建了一个新标记,将链接添加到新标记,然后尝试将该标记附加到另一个文档,但丢失了所有HTML格式:

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
print(image_link)
p_tag = soup.new_tag('p')
p_tag.append(soup.new_tag('br'))
p_tag.append(soup.new_tag('b'))
p_tag.b.append("Image: ")
p_tag.append(NavigableString(image_link))
print(p_tag)
返回:

<p><br/><b>Image: </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].</p>

图片:案例由Sachintha Hapugoda博士提供,a href=”https://radiopaedia.org/“Radiopaedia.org/a。从案例a href=”https://radiopaedia.org/cases/52525“rID:52525/a[于2018年11月15日查阅]


所有HTML格式都将丢失。我该怎么办?

因为
图像链接的类型是
导航链接
或字符串,它会像
一样转换字符,因为
图像链接的类型是
导航链接
或字符串,它会像
一样转换字符!你这个男人!马来西亚达里?我是一个业余爱好者,与BeautifulSoup和Scrapy一起做了很多工作,我正在考虑组建一个不和谐/松弛的团队。你有兴趣加入吗?老兄!你这个男人!马来西亚达里?我是一个业余爱好者,与BeautifulSoup和Scrapy一起做了很多工作,我正在考虑组建一个不和谐/松弛的团队。你有兴趣加入吗?
....
p_tag.b.append("Image: ")
image_tag = BeautifulSoup(image_link, 'html.parser')
p_tag.append(image_tag)
from html import unescape

....
p_tag.append(NavigableString(image_link))
unescaped_p = unescape(str(p_tag))
print(unescaped_p)