Python实现从doi号到bib文件

1. 简介

LaTeX的文献管理技能中,有个技能是:知道了doi号,如何得到bib文件。

2. 在线网站

3. Python代码实现

来自DOI to BibTeX的Python3代码doi2bib.py

import sys
import urllib.request
from urllib.error import HTTPError

BASE_URL = 'http://dx.doi.org/'

try:
    doi = sys.argv[1]
except IndexError:
    print('Usage:\n{} <doi>'.format(sys.argv[0]))
    sys.exit(1)

url = BASE_URL + doi
req = urllib.request.Request(url)
req.add_header('Accept', 'application/x-bibtex')
try:
    with urllib.request.urlopen(req) as f:
        bibtex = f.read().decode()
    print(bibtex)
except HTTPError as e:
    if e.code == 404:
        print('DOI not found.')
    else:
        print('Service unavailable.')
    sys.exit(1)

示例:

$ python doi2bib.py 10.1007/s10712-020-09595-4

@article{Deng_2020,
	doi = {10.1007/s10712-020-09595-4},
	url = {https://doi.org/10.1007%2Fs10712-020-09595-4},
	year = 2020,
	month = {jul},
	publisher = {Springer Science and Business Media {LLC}},
	volume = {41},
	number = {5},
	pages = {1075--1099},
	author = {Xiao-Le Deng and Wen-Bin Shen and Michael Kuhn and Christian Hirt and Roland Pail},
	title = {Magnetic Curvatures of a Uniformly Magnetized Tesseroid Using the Cartesian Kernels},
	journal = {Surveys in Geophysics}
}

4. bash命令实现自动化

来自Automatic citation data retrieval from dx.doi.org的建议:

$ curl -LH "Accept: application/x-bibtex" http://dx.doi.org/10.1007/s10712-020-09595-4

@article{Deng_2020,
	doi = {10.1007/s10712-020-09595-4},
	url = {https://doi.org/10.1007%2Fs10712-020-09595-4},
	year = 2020,
	month = {jul},
	publisher = {Springer Science and Business Media {LLC}},
	volume = {41},
	number = {5},
	pages = {1075--1099},
	author = {Xiao-Le Deng and Wen-Bin Shen and Michael Kuhn and Christian Hirt and Roland Pail},
	title = {Magnetic Curvatures of a Uniformly Magnetized Tesseroid Using the Cartesian Kernels},
	journal = {Surveys in Geophysics}
}

a text file dois.txt with one DOI identifier on a line:

for i in $(cat dois.txt); do curl -LH "Accept: application/x-bibtex" http://dx.doi.org/$i >> output.bib; done

5. Alfred实现

Fetching BibTeX entry from DOI - Information Bytes, Installing the Zotero translate server is pretty straightforward

git clone --recurse-submodules https://github.com/zotero/translation-server
cd translation-server
npm install
npm start
JsonOutput=`curl -d $1 -H 'Content-Type: text/plain' http://127.0.0.1:1969/search`
BibTeXOutput=`curl -d "$JsonOutput" -H 'Content-Type: application/json' 'http://127.0.0.1:1969/export?format=bibtex'`
echo -n $BibTeXOutput

6. Reference