我设法从某个网站上抓取了以下数据,但无法进一步抓取每个页面上的图像参考。让我举例说明:
data = """
<div class="Answer">
1. Origin (O): <i>clavicular head - </i>sternal half of clavicle. <i>Sternal head - </i>sternum down to 7th rib & cartilages of true ribs & 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 & TERES MAJOR])</i></b></div><div>3. NS: medial & lateral pectoral n. </div><div>4. A: adducts & internally rotates arm; flexes shoulder. </div><div><img src="paste-7450347406b71a5e5c2e6dc2442ca630347acc64.jpg"><br></div><div><b>Image: </b>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> [Accessed 15 Nov. 2018].</div>
</div>
<div class="Answer">
1. O: outer, upper surface of ribs 3-5. <div>2. I: corocoid process of scapula. </div><div>3. NS: medial pectoral n.</div><div>4. A: lowers the lateral angle & protracts the scapula. </div><div><br></div><div><img src="paste-fbab2e102740a7713816f498946f8cd977010c8f.gif"><br></div><div><b>Image:</b> Case 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></div>
</div>
"""
从这些数据中,我需要在'Image:'之后的引用,也就是说,我需要:
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> [Accessed 15 Nov. 2018].
Case 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>
我需要将两个引用重新插入到另一个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"
但是它不起作用,我该怎么办?
答案 0 :(得分:0)
使用以下方法:
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>'
答案 1 :(得分:0)
我确定这不是一个聪明的代码,但我认为这会有所帮助。
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)