-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.py
More file actions
120 lines (88 loc) · 4.21 KB
/
main.py
File metadata and controls
120 lines (88 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
Writing citations for research papers is time-consuming! Luckily, Python can
access NCBI's database when we want to retrieve article information and help
create a citation in MLA format for the first five articles found.
Run the script in terminal:
$ python main.py
And then enter search terms to find the article you are looking for.
Enjoy!
"""
def pubmed_search(s):
# import dependencies
import requests
# this is the base url of NCBI's Entrez API
base_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/"
# accessing NCBI database
database = "pubmed"
# build query url that is used to retrieve search results
query_url = (
base_url + "esearch.fcgi?db=" + database + "&term=" + s + "&retmode=json"
)
# request in response for the search information in JSON format
response = requests.get(query_url).json()
# idList itself is a list, create a string with the same name
idList = response["esearchresult"]["idlist"]
# set condition if no articles are found
if len(idList) == 0:
print("No articles found! Please try another search term.")
# condition when article(s) are found
else:
# for loop to iterate the first five articles in idList
for article in idList[0:5]:
# count articles
total = len(idList[0:5])
# find the index of each article
i = idList.index(article)
# message to show the article search
if i == 0:
print("------------------------")
print("Beginning article search")
print("------------------------")
# create article count. python index starts at 0 so we need to add 1
article_count = int(i) + 1
# count number of each article
print(f"Retrieving articles {article_count} of {total}")
# base url to retrieve article summary
url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?"
# generate endpoint url from our search to retrieve information for each article
search_url = url + "db=" + database + "&id=" + article + "&retmode=json"
# request article information, formatted as json
search_response = requests.get(search_url).json()
# create empty list for storing multiple authors per article
author_list = []
# try and except loop to keep the loop running
try:
# data parsing from our search response
pubmed_id = search_response["result"][article]["uid"]
title = search_response["result"][article]["title"]
authors = search_response["result"][article]["authors"]
journal = search_response["result"][article]["source"]
pub_date = search_response["result"][article]["pubdate"]
volume = search_response["result"][article]["volume"]
issue = search_response["result"][article]["issue"]
pages = search_response["result"][article]["pages"]
doi = search_response["result"][article]["elocationid"]
# append values to list
for i in authors:
all_authors = i["name"]
author_list.append(all_authors)
# replace [,], and ' symbols from each element in author list
names = (
str(author_list).replace("'", "").replace("[", "").replace("]", "")
)
# replace italization in title with <i></i>
corrected_title = title.replace("<i>", "<i>").replace(
"</i>", "</i>"
)
# show results
print(f"PubMed ID: {pubmed_id}")
print(
f"{names}. {corrected_title} {journal} {pub_date[0:4]};{volume}({issue}):{pages}. {doi}"
)
print("------------------------")
# create exception
except ValueError:
print("No information found. Skipping..")
# asks user for search terms
term = input("Please enter a search for PubMed articles: ").replace(" ", "+")
pubmed_search(term)