nik nik - 1 year ago 73
Ruby Question

how to get abstract from pubmed using PUBMEDid

I manually go to pubmed and for example search my topic for example then from summery I get PMIDs. and then try to retrieve all the abstracts using the following command.

I want to know two things, how to not do the first part manually? (means I do it also through scripting) and also how to save the abstracts ?

#Retrieve abstracts from PUBMEDid list
for i in `cat pmid.txt`;
do echo -n "$count";
ruby -e 'print "\t"';
echo -n $i;
ruby -e 'print "\n"';
curl "$i/abstract";
ruby -e 'print "\n"';


You can get the list of PMIDS by webscraping using mechanize gem in ruby. Do gem install mechanize and then you can get the required result by running the ruby script below:

require 'mechanize'
agent =
elements = agent.get('').search(".rprtid").to_a
pmids ={|x| x.elements.last.text}
puts "List of pmids:"
puts pmids "output_pmid_abstracts.txt", "w" ) do |file|
    for pmid in pmids
        puts "Getting Abstract for PMID: #{pmid}"
        abstract = agent.get("{pmid}/abstract").body
        file.puts "pmid:#{pmid}"
        file.puts abstract
        file.puts ""
puts "Done"

This will make output_pmid_abstracts.txt file in your current directory which will look something like below:

BACKGROUND & AIMS: Gastric cancer has familial clustering in incidence, and the familial relatives of gastric ...
Trefoil factor family (TFF) peptides are a group of molecules bearing a characteristic three-loop trefoil domain ...

PS: Please make sure that you absolutely need to install mechanize gem first! Or else you will obviously end up getting error: require': cannot load such file -- mechanize (LoadError), because it is not able to find the required library/gem. By any case if even after gem install mechanize you get require error, then do sudo gem install mechanize and then try.

Hope it helps : )