Fetching Google Search using MATLAB

show search results from google to matlab

Google Search using MATLAB or Displaying Top 10 Google Results in MATLAB

Google Search using MATLAB, and displaying the first 10 results from the Google search engine in MATLAB window. This code is tested on MATLAB 2012b, but it is expected to work on all newer versions.

show search results from google to matlab

Code for MATLAB:

% GOOGLE SEARCH USING MATLAB% Working on Web page reading

% Tested on: October 18, 2013

% wtitle = Title of search Results
% wresults = No of results found
% Designed By: Engr. Azam Rafique Memon
% Instrumentation Engineer (TQCIC), MUET
% Is now the property of:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% COMMITTED TO EXCELLENCE %
% www.comm2excel.com %
% info@comm2excel.com %
% Copyright(C) 2013, comm2excel.com %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

a=input('Search String: ');
a=regexprep(a,' ','%20');
%a='site:comm2excel.com';
urlp=['http://www.google.com.pk/search?hl=en&q=' a];
%urlp=['http://www.google.com.pk'];
txt=urlread(urlp); % to Read url and show its code
urlwrite(urlp,'tst2.html');

% TITLE OF SEARCH PAGE
wtitle=regexp(txt,'<title>[^\t]*</title>','match'); % get title of page opened
wtitle=regexprep(wtitle,'<title>','');
wtitle=regexprep(wtitle,'</title>','');
%%%%%%%%%%%%%%%%%%%%%%%%%%

% Total Number of Results Found
wresults=regexp(txt,'<div id="resultStats">[A-Za-z0-9, ]*','match'); % to get no of results found
wresults=regexprep(wresults,'<div id="resultStats">','');
wresults=regexprep(wresults,'<nobr>','')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%URLs of Titles (redirected from google)
rurl=regexp(txt,'<h3 class=\"r\"><a href="/url\?q=(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^!=%&amp;/~\+#])?','match');
rurl=regexprep(rurl,'/url\?q=','https://www.google.com.pk/url\?q=');
rurl=regexprep(rurl,'<h3 class="r"><a href="','');
rurl=char(rurl);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% URLs mentioned below TITLE
rurl2=regexp(txt,'>(<cite>|<cite class="kv">)[a-zA-Z0-9\.\/()\_\-<>\?\=\%]*</cite>?','match');
rurl2=regexprep(rurl2,'><cite>','');
rurl2=regexprep(rurl2,'><cite class="kv">','');
rurl2=regexprep(rurl2,'</cite>','');
rurl2=regexprep(rurl2,'<b>','');
rurl2=regexprep(rurl2,'</b>','');
rurl2=char(rurl2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rtitle=regexp(txt,'">[a-zA-Z0-9(\s)*!?@\.,<>//\\":\-&^%$#@!;\|(--)*]*</h3>','match');
rtitle=regexprep(rtitle,'">','');
rtitle=regexprep(rtitle,'<b>','');
rtitle=regexprep(rtitle,'</b>','');
rtitle=regexprep(rtitle,'<em>','');
rtitle=regexprep(rtitle,'</em>','');
rtitle=regexprep(rtitle,' class=l>','');
rtitle=regexprep(rtitle,'</h3>','');
rtitle=regexprep(rtitle,'</a>','');
rtitle=regexprep(rtitle,'<a','');
rtitle=regexprep(rtitle,'<b>...</b>','');
rtitle=char(rtitle);

if(length(rurl2(:,1) > length(rtitle(:,1))))
n=length(rurl2(:,1));
else
n=length(rtitle(:,1));
end

for i=1:n
%disp(['Title: ' rtitle(i,:)])
%disp(['URL: ' rurl(i,:)])
disp(['Title: <a href="' rurl2(i,:) '">' rtitle(i,:) '</a>'])

end

 

Results:

These are the results using MATLAB 2012b.

Search results from Google to Matlab

 

Video on Searching Google using MATLAB & Showing Results in MATLAB Window:

Downloads:

 

* This article was earlier posted on comm2excel.com, which has been closed now and all the data from that is being transferred to our new domain galaxysofts.com.

* This code is the same as it was posted on comm2excel.com, a few years earlier. However, it is not tested recently whether it works or not now because Google keeps changing its layout and Matlab codes are also being updated.

Leave a Reply

Your email address will not be published.