Skip to content

Instantly share code, notes, and snippets.

@hubgit
Last active August 16, 2022 09:08
Show Gist options
  • Save hubgit/d6ae523f74f515ecd05d to your computer and use it in GitHub Desktop.
Save hubgit/d6ae523f74f515ecd05d to your computer and use it in GitHub Desktop.
PubMed search with jQuery
<!doctype html>
<meta charset="utf-8">
<title>PubMed Search</title>
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<ul id="output"></ul>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="pubmed-search.js"></script>
searchPubMed('FOXP3 AND 2013/01:2014/01[Publication Date]')
.then(fetchResults)
.then(parseResults)
.then(displayResults);
function searchPubMed(term) {
return $.ajax({
url: 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi',
data: {
db: 'pubmed',
usehistory: 'y',
term: term,
retmode: 'json',
retmax: 0
}
});
}
function fetchResults(response) {
return $.ajax({
url: 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi',
data: {
db: 'pubmed',
usehistory: 'y',
webenv: response.esearchresult.webenv,
query_key: response.esearchresult.querykey,
retmode: 'xml',
retmax: 10 // how many items to return
}
});
}
function parseResults(response) {
var nodes = response.querySelectorAll('DocSum');
return $.map(nodes, function(node) {
var pmidNode = node.querySelector('Id');
var doiNode = node.querySelector('Item[Name=DOI]');
var titleNode = node.querySelector('Item[Name=Title]');
var sourceNode = node.querySelector('Item[Name=Source]');
var epubDateNode = node.querySelector('Item[Name=EPubDate]');
var pubDateNode = node.querySelector('Item[Name=PubDate]');
var authorNodes = node.querySelectorAll('Item[Name=AuthorList] > Item[Name=Author]');
return {
title: titleNode ? titleNode.textContent : null,
source: sourceNode ? sourceNode.textContent : null,
authors: $.map(authorNodes, function(authorNode) {
return authorNode.textContent;
}),
url: doiNode ? 'http://dx.doi.org/' + encodeURIComponent(doiNode.textContent) : 'http://pubmed.gov/' + pmidNode.textContent,
date: epubDateNode && epubDateNode.textContent ? epubDateNode.textContent : pubDateNode.textContent,
};
});
}
function displayResults(articles) {
var output = $('#output');
$.each(articles, function (i, article) {
var item = $('<li/>').appendTo(output);
var container = $('<div/>').appendTo(item);
$('<a/>', {
href: article.url,
text: article.title
}).appendTo(container);
$('<div/>', {
text: article.authors.join(', ')
}).appendTo(item);
$('<div/>', {
text: article.date + ' · ' + article.source
}).appendTo(item);
});
}
@jenzopr
Copy link

jenzopr commented Feb 22, 2018

Thanks a lot! This is great!

@ikgithub17
Copy link

ikgithub17 commented May 2, 2019

I try to search blood pressure but an exception thrown
Returned JSON
{"header":{"type":"esearch","version":"0.3"},"esearchresult":{"count":"593071","retmax":"0","retstart":"0","querykey":"1","webenv":"NCID_1_265393899_130.14.22.76_9001_1556770035_288546446_0MetA0_S_MegaStore","idlist":[],"translationset":[{"from":"blood pressure","to":""blood pressure"[MeSH Terms] OR ("blood"[All Fields] AND "pressure"[All Fields]) OR "blood pressure"[All Fields] OR "blood pressure determination"[MeSH Terms] OR ("blood"[All Fields] AND "pressure"[All Fields] AND "determination"[All Fields]) OR "blood pressure determination"[All Fields] OR ("blood"[All Fields] AND "pressure"[All Fields]) OR "blood pressure"[All Fields] OR "arterial pressure"[MeSH Terms] OR ("arterial"[All Fields] AND "pressure"[All Fields]) OR "arterial pressure"[All Fields] OR ("blood"[All Fields] AND "pressure"[All Fields])"}],"translationstack":[{"term":""blood pressure"[MeSH Terms]","field":"MeSH Terms","count":"281483","explode":"Y"},{"term":""blood"[All Fields]","field":"All Fields","count":"3686199","explode":"N"},{"term":""pressure"[All Fields]","field":"All Fields","count":"1124535","explode":"N"},"AND","GROUP","OR",{"term":""blood pressure"[All Fields]","field":"All Fields","count":"428474","explode":"N"},"OR",{"term":""blood pressure determination"[MeSH Terms]","field":"MeSH Terms","count":"37604","explode":"Y"},"OR",{"term":""blood"[All Fields]","field":"All Fields","count":"3686199","explode":"N"},{"term":""pressure"[All Fields]","field":"All Fields","count":"1124535","explode":"N"},"AND",{"term":""determination"[All Fields]","field":"All Fields","count":"505774","explode":"N"},"AND","GROUP","OR",{"term":""blood pressure determination"[All Fields]","field":"All Fields","count":"26732","explode":"N"},"OR",{"term":""blood"[All Fields]","field":"All Fields","count":"3686199","explode":"N"},{"term":""pressure"[All Fields]","field":"All Fields","count":"1124535","explode":"N"},"AND","GROUP","OR",{"term":""blood pressure"[All Fields]","field":"All Fields","count":"428474","explode":"N"},"OR",{"term":""arterial pressure"[MeSH Terms]","field":"MeSH Terms","count":"4646","explode":"Y"},"OR",{"term":""arterial"[All Fields]","field":"All Fields","count":"391797","explode":"N"},{"term":""pressure"[All Fields]","field":"All Fields","count":"1124535","explode":"N"},"AND","GROUP","OR",{"term":""arterial pressure"[All Fields]","field":"All Fields","count":"60038","explode":"N"},"OR",{"term":""blood"[All Fields]","field":"All Fields","count":"3686199","explode":"N"},{"term":""pressure"[All Fields]","field":"All Fields","count":"1124535","explode":"N"},"AND","GROUP","OR","GROUP"],"querytranslation":""blood pressure"[MeSH Terms] OR ("blood"[All Fields] AND "pressure"[All Fields]) OR "blood pressure"[All Fields] OR "blood pressure determination"[MeSH Terms] OR ("blood"[All Fields] AND "pressure"[All Fields] AND "determination"[All Fields]) OR "blood pressure determination"[All Fields] OR ("blood"[All Fields] AND "pressure"[All Fields]) OR "blood pressure"[All Fields] OR "arterial pressure"[MeSH Terms] OR ("arterial"[All Fields] AND "pressure"[All Fields]) OR "arterial pressure"[All Fields] OR ("blood"[All Fields] AND "pressure"[All Fields])"}}

Error Thrown: response.querySelectorAll is not a function
Error caused by this line
var nodes = response.querySelectorAll('DocSum');

thanks in advance for any advise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment