LaTeX로 논문을 작성할 때 참고문헌을 BibTeX 형식으로 변환해야 할 때가 있지 않나요? 문헌 수가 많다면 이런 반복 작업은 할 필요가 없습니다. EndNote나 Zotero 같은 문헌 관리 도구를 사용하면 한 번에 내보낼 수 있지만, 그런 도구가 없다면 이 글에서 해결책을 제공합니다.
솔루션: Crossref API + Google Scholar API
Crossref는 외문 DOI를 발행하는 플랫폼 중 가장 큰 곳으로, 거의 모든 외문 문헌의 메타데이터를 보유하고 있습니다. 하지만 arXiv를 비롯한 일부 문헌은 검색되지 않는 경우가 있어 이때는 Google Scholar의 도움이 필요합니다.
시간을 아끼기 위해 두 API를 이미 래핑해 두었으니 pip로 설치하기만 하면 됩니다.
pip install get_bibtex
이후 아래와 같이 사용하면 됩니다.
from apiModels.get_bibtex_from_crossref import GetBibTex
from apiModels.get_bibtex_from_google_scholar import GetBibTexFromGoogleScholar
if __name__ == '__main__':
google_scholar_api_key = "your_google_scholar_api_key"
get_bibtex_from_crossref = GetBibTex("1536727925@qq.com")
get_bibtex_from_google_scholar = GetBibTexFromGoogleScholar(google_scholar_api_key, GetBibTexFromGoogleScholar.APA)
with open("inputfile/Bibliographyraw.txt", "r", encoding='utf-8') as f:
raws = f.readlines()
# get bibtex from CrossRef and failed search results
success_bibtexs_crossref, failed_results = get_bibtex_from_crossref.get_bibtexs(raws)
# for each failed search result, get bibtex from Google Scholar
success_bibtexs_google, failed_results = get_bibtex_from_google_scholar.get_bibtexs(failed_results)
with open("outputfile/BibliographyCrossRef.txt", "w", encoding='utf-8') as f:
for bibtex in success_bibtexs_crossref:
f.write(bibtex)
with open("outputfile/BibliographyGoogleScholar.txt", "w", encoding='utf-8') as f:
for index, bibtex in enumerate(success_bibtexs_google):
f.write("[]".format(index) + " " + bibtex + "\n")
with open("outputfile/not_find.txt", "w", encoding='utf-8') as f:
for result in failed_results:
f.write(result+"\n")
print("find bibtex from CrossRef: ", len(success_bibtexs_crossref))
print("find bibtex from Google Scholar: ", len(success_bibtexs_google))
print("not find: ", len(failed_results))
핵심 코드 설명
Python
Bibliographyraw.txt里面是需要查询的文件
例如:
J. Hu, L. Shen, S. Albanie, G. Sun, and A. Vedaldi, “Gather-Excite: Exploiting Feature Context in Convolutional Neural Networks.” arXiv, Jan. 12, 2019. doi: 10.48550/arXiv.1810.12348.
X. Wang, R. Girshick, A. Gupta, and K. He, “Non-local Neural Networks.” arXiv, Apr. 13, 2018. doi: 10.48550/arXiv.1711.07971.
------------------
Python
success_bibtexs_crossref, failed_results = get_bibtex_from_crossref.get_bibtexs(raws)
返回的第一个参数为bibtex列表,第二个为没有查询到的原文献
Python
success_bibtexs_google, failed_results = get_bibtex_from_google_scholar.get_bibtexs(failed_results)
将没有查到的文献继续使用google API查询,一般都是可以查询到了,没有文献在google scholar查询不到了吧
Python
提示,这里返回的其实是APA格式的,在上面初始化指定,也有一个参数可以设置返回bibtex格式,例如
get_bibtex_from_google_scholar = GetBibTexFromGoogleScholar(google_scholar_api_key, GetBibTexFromGoogleScholar.APA, flag = True)
但是需要设置代理服务器,例如:
import os
import re
import requests
os.environ["http_proxy"]="127.0.0.1:7890"
os.environ["https_proxy"]="127.0.0.1:7890"
!!!!!!!먼저 API를 신청해야 합니다. 매달 100회의 무료 검색이 제공되며 보통은 충분합니다. serpapi.com에서 신청하세요.
이후 코드는 한눈에 보일 겁니다 ㅎㅎ
단일 검색을 요청하는 방법도 있습니다.
get_bibtex() 去掉s就可以了
2024.4.16 업데이트
1、DBLP 인터페이스 추가
Python
from apiModels.get_bibtex_from_dblp import GetBibTexFromDBLP
2、사용 편의성 향상
Python
现在提供一个封装好的类提供使用,这个方法已经封装好了Crosref和DBLP的API
from apiModels.workflow.crossref2dblp import Crossref2Dblp
使用方法(无google scholar API)
crossref2dblp = Crossref2Dblp("your email", "inputfile/Bibliographyraw.txt", "outputfile/Bibliography.txt")
crossref2dblp.running()
坐等运行完成
(有了google scholar API)
from apiModels.workflow.crossref2dblp import Crossref2Dblp
from apiModels.get_bibtex_from_google_scholar import GetBibTexFromGoogleScholar
get_bibtex_from_google_scholar = GetBibTexFromGoogleScholar(api_key="your api key")
在最后面参数加上你封装的API
crossref2dblp = Crossref2Dblp("1536727925@qq.com", "inputfile/Bibliographyraw.txt", "outputfile/Bibliography.txt",get_bibtex_from_google_scholar)
crossref2dblp.running()
坐等运行完成
或者你想自己定义api之间的调用顺序
from apiModels.workflow.make_workflow import MakeWorkflow
from apiModels.get_bibtex_from_google_scholar import GetBibTexFromGoogleScholar
from apiModels.get_bibtex_from_crossref import GetBibTex
get_bibtex_from_google_scholar = GetBibTexFromGoogleScholar(api_key="your api key")
get_bibtex_from_crossref = GetBibTex("1536727925@qq.com")
make_workflow = MakeWorkflow("inputfile/Bibliographyraw.txt", "outputfile/Bibliography.txt", get_bibtex_from_google_scholar, get_bibtex_from_crossref)
make_workflow.running()
使用之前:
pip install get_bibtex = 1.1.0
개선 제안 환영합니다.