无法使用BeautifulSoup从HTML提取引用

2024-09-30 03:21:57 发布

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

我设法从一个网站上抓取了以下数据,但无法进一步抓取每个页面上的图像引用。让我举例说明:

data = """
<div class="Answer">
1. Origin (O): <i>clavicular head -&nbsp;</i>sternal half of clavicle. <i>Sternal head -&nbsp;</i>sternum down to 7th rib &amp; cartilages of true ribs &amp; aponeurosis of EXTERNAL OBLIQUE.<div>2. Insertion (I): lateral lip of intertubercular sulcus of humerus <b><i>(TIP: 1 missus [LATISSIMUS DORSI] b/w 2 majors [PECTORALIS MAJOR &amp; TERES MAJOR])</i></b></div><div>3. NS: medial &amp; lateral pectoral n.&nbsp;</div><div>4. A: adducts &amp; internally rotates arm; flexes shoulder.&nbsp;</div><div><img src="paste-7450347406b71a5e5c2e6dc2442ca630347acc64.jpg"><br></div><div><b>Image:&nbsp;</b>Gray, Henry. <i>Anatomy of the Human Body.</i> Philadelphia: Lea &amp; Febiger, 1918; Bartleby.com, 2000. <a href="https://www.bartleby.com/107/">www.bartleby.com/107/</a>&nbsp;[Accessed 15 Nov. 2018].</div>
</div>
<div class="Answer">
1. O: outer, upper surface of ribs 3-5.&nbsp;<div>2. I: corocoid process of scapula.&nbsp;</div><div>3. NS: medial pectoral n.</div><div>4. A: lowers the lateral angle &amp; protracts the scapula.&nbsp;</div><div><br></div><div><img src="paste-fbab2e102740a7713816f498946f8cd977010c8f.gif"><br></div><div><b>Image:</b>&nbsp;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/52195"&gt;rID: 52195&lt;/a&gt;</div>
</div>
"""

根据这些数据,我需要“Image:”后面的引用,也就是说,我需要:

Gray, Henry. <i>Anatomy of the Human Body.</i> Philadelphia: Lea &amp; Febiger, 1918; Bartleby.com, 2000. <a href="https://www.bartleby.com/107/">www.bartleby.com/107/</a>&nbsp;[Accessed 15 Nov. 2018].

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/52195"&gt;rID: 52195&lt;/a&gt;

我需要将这两个引用插入到另一个HTML页面中。 我试过这个:

soup = BeautifulSoup(data, "html.parser")
Answers = soup.find_all("div", {"class":"Answer"})
for answer in Answers:
    if answer.find('b').next == 'Image:': image_link = BeautifulSoup(answer.find('b').next.next, 'html.parser')
    else: image_link = "no link"

但没用,我能怎么办?你知道吗


Tags: ofthehttpsorgimageltgtdiv
2条回答

我相信这不是一个聪明的代码,但我认为它会有所帮助

soup = BeautifulSoup(data, "html.parser")
Answers = soup.find_all("div", {"class":"Answer"})
for answer in Answers:
   regex1 = r"<div><b>.*?</b>"
   regex2 = r"</div>"
   subst = ""
   if answer.find_all('b')[-1].next.strip() == 'Image:':
       parent_element = answer.find_all('b')[-1].parent
       result = re.sub(regex1, subst, str(parent_element))
       image_link = re.sub(regex2, subst, str(result))
   else: 
       image_link = "no link"

print(image_link)

使用以下方法:

soup = BeautifulSoup(data, "html.parser")
img_links = soup.select('div.Answer b')

for el in img_links:
    print(''.join(map(repr, el.next_siblings)))

输出:

'Gray, Henry. '<i>Anatomy of the Human Body.</i>' Philadelphia: Lea & Febiger, 1918; Bartleby.com, 2000. '<a href="https://www.bartleby.com/107/">www.bartleby.com/107/</a>'\xa0[Accessed 15 Nov. 2018].'
'\xa0Case courtesy of Dr Sachintha Hapugoda, <a href="https://radiopaedia.org/">Radiopaedia.org</a>. From the case <a href="https://radiopaedia.org/cases/52195">rID: 52195</a>'

相关问题 更多 >

    热门问题