<jupyter_start><jupyter_text>Get doi.org BibTex record from Crossref DOI_Committed by <NAME> (@tomohenstein) 2016-09-13_<jupyter_code>import xmltodict
import requests 
import json
### define function for making a crossref query to get doi 

def getDOI(title): 
    url = "http://api.crossref.org/works?"
    a = "query.author=Wong"
    title.replace(" ", "+")
    t = "&query.title=" + title
    query = url + a + t 
    r = requests.get(query)
    j = json.loads(r.text)
    doi = j["message"]["items"][0]["DOI"]
    return doi
### function for taking crossref doi and getting bibtex 
###  from https://gist.github.com/jrsmith3/5513926 

def doi2bib(doi):
    url = "http://dx.doi.org/" + doi
    headers = {"accept": "application/x-bibtex"}
    r = requests.get(url, headers = headers)
    return r.text
### function to add new citation to bibtex file 
### load xml file 
with open("WongJY_NCBI.xml") as f: 
    doc = xmltodict.parse(f.read())
### loop through articles 
counter = 0
for item in doc["DocumentSummarySet"]["DocumentSummary"]:
    title = item['Item']["Title"]
    ## call crossref query function 
    doi = getDOI(title)
    ## call doi2bib function 
    bibtex = doi2bib(doi)
    with open("wong.bib", "a") as myfile:
        myfile.write(bibtex)
    counter = counter + 1
    print(counter)<jupyter_output>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
StarcoderdataJupyterStructuredCleanDedup