这很草率,我想听听反馈,但这里的代码认为最新发布的 id 与最新论文相同(我不确定这是真的)。基本上对最新的 PMID 进行二进制搜索,然后给出最新的列表n
。这不看日期,只返回 PMID,所以我不确定这是一个合适的答案,但也许可以调整这个想法。
代码:
import urllib2
def pmid_exists(pmid):
url_stem = 'https://www.ncbi.nlm.nih.gov/pubmed/'
query = url_stem+str(pmid)
try:
request = urllib2.urlopen(query)
return True
except urllib2.HTTPError:
return False
def get_latest_pmid(max_exists = 27239557, min_missing = -1):
#print max_exists,'-->',min_missing
if abs(min_missing-max_exists) <= 1:
return max_exists
guess = (max_exists+min_missing)/2
if min_missing == -1:
guess = 2*max_exists
if pmid_exists(guess):
return get_latest_pmid(guess, min_missing)
else:
return get_latest_pmid(max_exists, guess)
#Start of program
if __name__ == '__main__':
n = 5
latest_pmid = get_latest_pmid()
most_recent_n_pmids = range(latest_pmid-n, latest_pmid)
print most_recent_n_pmids
输出:
[28245638, 28245639, 28245640, 28245641, 28245642]