-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
95 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/bin/bash | ||
|
||
TASK=$1 | ||
|
||
# pubmed_parser | ||
if [ "$TASK" = "pubmed" ]; then | ||
echo "Install pubmed_parser and dependencies" | ||
pip install git+https://github.com/titipata/pubmed_parser.git | ||
pip install ebooklib beautifulsoup4 lxml | ||
fi | ||
|
||
echo "Done!" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"""Get text from pubmed id""" | ||
|
||
import ebooklib | ||
from ebooklib import epub | ||
from bs4 import BeautifulSoup | ||
import pubmed_parser as pp | ||
import requests | ||
import tempfile | ||
|
||
|
||
def medline2text(mlid): | ||
outputs = pp.parse_xml_web(mlid, save_xml=False) | ||
return outputs["title"], outputs["abstract"] | ||
|
||
|
||
def pubmed2text(pmid): | ||
headers = { | ||
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"} | ||
response = requests.get(url=f"https://www.ncbi.nlm.nih.gov/pmc/articles/{pmid}/epub/", headers=headers) | ||
response.raise_for_status() | ||
|
||
with tempfile.NamedTemporaryFile() as epub_file: | ||
epub_file.write(response.content) | ||
epub_file.flush() | ||
|
||
article = epub.read_epub(epub_file.name) | ||
|
||
for item in article.get_items_of_type(ebooklib.ITEM_DOCUMENT): | ||
if pmid[3:] in item.get_name(): | ||
markup = BeautifulSoup(item.get_content(), "lxml-xml") | ||
|
||
title_tag = markup.find("span", class_="article-title") | ||
title = title_tag.string.strip() if title_tag else "N/A" | ||
|
||
abstract_tag = markup.find("div", class_="abstract") | ||
abstract = abstract_tag.get_text().strip() if abstract_tag else "N/A" | ||
|
||
body_tag = markup.find("div", class_="body") | ||
sec_tags = body_tag and body_tag.find_all("div", class_="sec") | ||
content = "\n".join(sec_tag.get_text().strip() for sec_tag in sec_tags) if sec_tags else "N/A" | ||
|
||
return title, abstract, content | ||
|
||
|
||
def main(): | ||
|
||
# medline | ||
title, abstract = medline2text("18483370") | ||
print("Title: \n", title) | ||
print("Abstract: \n", abstract) | ||
|
||
# pubmed | ||
title, abstract, content = pubmed2text("PMC441591") | ||
print("Title: \n", title) | ||
print("Abstract: \n", abstract) | ||
print("Content: \n", content) | ||
|
||
return | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |