我已经使用BeautifulSoup从以下网站获取了医学文档的正文:
import requests
r = requests.get('https://www.mtsamples.com/site/pages/sample.asp?
Type=24-Gastroenterology&Sample=2332-Abdominal%20Abscess%20I&D')
from bs4 import BeautifulSoup
soup = BeautifulSoup(r.text, 'html.parser')
results = soup.find_all('div', attrs={'id':'sampletext'})
body_text = soup.find('body').text
从中抓取文本的网页没有设计任何语义标记,只是带有粗体标题的一大块文本。正文如下:
'\n\n\n\n\n \nTranscribed Medical Transcription Sample Reports and Examples\n\n\n\n\n\n\n\n\n\n\n\r\n(adsbygoogle = window.adsbygoogle || []).push({});\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n|\n\n\n\n\n\nvar addthis_config = {"data_track_clickback":true};\nView this sample in Blog format on MedicalTranscriptionSamples.com\n\n\n\n\n\n\n\n\n\n\n\n\r\n(adsbygoogle = window.adsbygoogle || []).push({});\r\n\n\n\n\n\nvar addthis_config = {"data_track_clickback":true};\n\n\n Sample Type / Medical Specialty: Gastroenterology\nSample Name: Abdominal Abscess I&D \n Description: Incision and drainage (I&D) of abdominal abscess, excisional debridement of nonviable and viable skin, subcutaneous tissue and muscle, then removal of foreign body.\r\n (Medical Transcription Sample Report)\n\nPREOPERATIVE DIAGNOSIS: Abdominal wall abscess.POSTOPERATIVE DIAGNOSIS: Abdominal wall abscess.PROCEDURE: Incision and drainage (I&D) of abdominal abscess, excisional debridement of nonviable and viable skin, subcutaneous tissue and muscle, then removal of foreign body.ANESTHESIA: LMA.INDICATIONS: Patient is a pleasant 60-year-old gentleman, who initially had a sigmoid colectomy for diverticular abscess, subsequently had a dehiscence with evisceration. Came in approximately 36 hours ago with pain across his lower abdomen. CT scan demonstrated presence of an abscess beneath the incision. I recommended to the patient he undergo the above-named procedure. Procedure, purpose, risks, expected benefits, potential complications, alternatives forms of therapy were discussed with him, and he was agreeable to surgery.FINDINGS: The patient was found to have an abscess that went down to the level of the fascia. The anterior layer of the fascia was fibrinous and some portions necrotic. This was excisionally debrided using the Bovie cautery, and there were multiple pieces of suture within the wound and these were removed as well.TECHNIQUE: Patient was identified, then taken into the operating room, where after induction of appropriate anesthesia, his abdomen was prepped with Betadine solution and draped in a sterile fashion. The wound opening where it was draining was explored using a curette. The extent of the wound marked with a marking pen and using the Bovie cautery, the abscess was opened and drained. I then noted that there was a significant amount of undermining. These margins were marked with a marking pen, excised with Bovie cautery; the curette was used to remove the necrotic fascia. The wound was irrigated; cultures sent prior to irrigation and after achievement of excellent hemostasis, the wound was packed with antibiotic-soaked gauze. A dressing was applied. The finished wound size was 9.0 x 5.3 x 5.2 cm in size. Patient tolerated the procedure well. Dressing was applied, and he was taken to recovery room in stable condition.\r\n \n\n\n\n\n\r\n (adsbygoogle = window.adsbygoogle || []).push({});\r\n\n\n\n\n\n\n\n\n\r\n(adsbygoogle = window.adsbygoogle || []).push({});\r\n\n\n\nKeywords: \r\n gastroenterology, excisional debridement, subcutaneous tissue, abdominal wall abscess, foreign body, abdominal abscess, bovie cautery, abdominal, i&d, wound, incision, abscess, \r\n \r\n \n\n\n\n\n\n\n\n\n\n\n \n\n\nNOTE: These\r\n transcribed medical transcription sample reports and examples are provided by various users and\r\n are for reference purpose only. MTHelpLine does not certify accuracy and quality of sample reports.\r\n These transcribed medical transcription sample reports may include some uncommon or unusual formats;\r\n this would be due to the preference of the dictating physician. All names and dates have been\r\n changed (or removed) to keep confidentiality. Any resemblance of any type of name or date or\r\n place or anything else to real world is purely incidental.\n\n\n\n\n\nHome | MedicalTranscriptionSamples | MT911 | MTDictionary | MTSetup | MTHelpLine\nSitemap | Tell\r\n a Friend | Contact\r\n Us | Disclaimer\n\n\n\n\nTranscribed Medical Transcription Sample Reports and Examples\n\n\n\n\n\r\n (function(i,s,o,g,r,a,m){i[\'GoogleAnalyticsObject\']=r;i[r]=i[r]||function(){\r\n (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),\r\n m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)\r\n })(window,document,\'script\',\'https://www.google-analytics.com/analytics.js\',\'ga\');\r\n\r\n ga(\'create\', \'UA-1275448-3\', \'auto\');\r\n ga(\'send\', \'pageview\');\r\n\r\n\n'
我想提取文档文本的子字符串,该子字符串以Sample Type / Medical Specialty
字段开始,以TECHNIQUE
字段结束。为了提取文本的此子字符串,我尝试使用正则表达式直接在Sample Type / Medical Specialty
字段之前和TECHNIQUE
字段之后解析字符:
import re
try:
found = re.search('addthis_config =
{"data_track_clickback":true};\n\n\n(.+?)\r\n \n\n\n\n\n\r\n
(adsbygoogle = window.adsbygoogle || []', body_text).group(1)
except AttributeError:
# Start/Stop markers not found in the original string
found = ''
很显然,这是错误的,但希望我能理解我要寻找共同的正文标记的开始和结束标记。如果您对如何实现此方法有任何反馈或建议,我将不胜感激。
答案 0 :(得分:0)
答案 1 :(得分:0)
答案 2 :(得分:0)
此页面为半结构化,每个块均以<b>
标记开头,该标记包含标题,后跟":"
符号。我们通过next_sibling()
调用来获取此标题和下一个兄弟姐妹,您将获得数据:
import requests
from bs4 import BeautifulSoup
url = 'https://www.mtsamples.com/site/pages/sample.asp?Type=24-Gastroenterology&Sample=2332-Abdominal%20Abscess%20I&D'
soup = BeautifulSoup(requests.get(url).text, 'lxml')
data = [(title.get_text(strip=True).replace(':', ''), title.next_sibling.strip()) for title in soup.select('#sampletext b:contains(":")')]
from textwrap import wrap
for (title, text) in data[:-1]: # -1 because we don't want last 'Keywords' row
print('Title:', title)
print('Text:')
print('\n'.join('\t' + t for t in wrap(text, width=70)))
print('*' * 80)
打印:
Title: Sample Type / Medical Specialty
Text:
Gastroenterology
********************************************************************************
Title: Sample Name
Text:
Abdominal Abscess I&D
********************************************************************************
Title: Description
Text:
Incision and drainage (I&D) of abdominal abscess, excisional
debridement of nonviable and viable skin, subcutaneous tissue and
muscle, then removal of foreign body.
********************************************************************************
Title: PREOPERATIVE DIAGNOSIS
Text:
Abdominal wall abscess.
********************************************************************************
Title: POSTOPERATIVE DIAGNOSIS
Text:
Abdominal wall abscess.
********************************************************************************
Title: PROCEDURE
Text:
Incision and drainage (I&D) of abdominal abscess, excisional
debridement of nonviable and viable skin, subcutaneous tissue and
muscle, then removal of foreign body.
********************************************************************************
Title: ANESTHESIA
Text:
LMA.
********************************************************************************
Title: INDICATIONS
Text:
Patient is a pleasant 60-year-old gentleman, who initially had a
sigmoid colectomy for diverticular abscess, subsequently had a
dehiscence with evisceration. Came in approximately 36 hours ago with
pain across his lower abdomen. CT scan demonstrated presence of an
abscess beneath the incision. I recommended to the patient he undergo
the above-named procedure. Procedure, purpose, risks, expected
benefits, potential complications, alternatives forms of therapy were
discussed with him, and he was agreeable to surgery.
********************************************************************************
Title: FINDINGS
Text:
The patient was found to have an abscess that went down to the level
of the fascia. The anterior layer of the fascia was fibrinous and
some portions necrotic. This was excisionally debrided using the
Bovie cautery, and there were multiple pieces of suture within the
wound and these were removed as well.
********************************************************************************
Title: TECHNIQUE
Text:
Patient was identified, then taken into the operating room, where
after induction of appropriate anesthesia, his abdomen was prepped
with Betadine solution and draped in a sterile fashion. The wound
opening where it was draining was explored using a curette. The
extent of the wound marked with a marking pen and using the Bovie
cautery, the abscess was opened and drained. I then noted that there
was a significant amount of undermining. These margins were marked
with a marking pen, excised with Bovie cautery; the curette was used
to remove the necrotic fascia. The wound was irrigated; cultures sent
prior to irrigation and after achievement of excellent hemostasis, the
wound was packed with antibiotic-soaked gauze. A dressing was
applied. The finished wound size was 9.0 x 5.3 x 5.2 cm in size.
Patient tolerated the procedure well. Dressing was applied, and he
was taken to recovery room in stable condition.
********************************************************************************
编辑:为了获取包含<b>
字符的:
标签,我使用CSS选择器#sampletext b:contains(":")
。
进一步阅读: