How can I access the first link of google's search bar through matlab?

4 views (last 30 days)
So lets say I want to search for Mathworks on google via matlab, a code that could do this could be like this
search_target = 'mathworks';
url = ['www.google.com/search?q=' search_target];
%url = ['www.google.com/search?btnI=1&q=' search_target];
web(url,'-browser')
Now my question is how can I access the first link the code generated or to clarify what can I add to the code so it can automatically access the first link from the generated search query?
  4 Comments
Geoff Hayes
Geoff Hayes on 23 Jun 2020
Ahd - take a look at the link I've included in my comment. The code would look something like
search_target = 'mathworks';
url = ['www.google.com/search?q=' search_target];
code = webread(url);
tree = htmlTree(code);
and then you would need to "traverse" the tree to (presumably) find the first link, extract it and then navigate to it using web. I haven't tried to do this (my version of MATLAB is too old) but I think it is doable.

Sign in to comment.

Answers (2)

Rafael S.T. Vieira
Rafael S.T. Vieira on 23 Jun 2020
Edited: Rafael S.T. Vieira on 23 Jun 2020
Hi, Mukbil,
Using webread only, we would have an html-document, and have to use regexp to find whatever we want:
url=['https://www.google.com/search'];
html = webread(url, 'q', 'mathworks');
tokens = regexp(html , 'href="/url\?q=(?<links>[^ >]+)/', 'names');
first=tokens(1).links;
disp(first)
As you can imagine, Google is not very fond of people doing this. We could skip accessing their site (and ads), so the previous code with regexp may work today, but may not work tomorrow.
A better solution is to use REST for accessing the Google API. They will give you a unique key that you can employ at your application and a link that will return a JSON file with the results.
And as it was said already, another good option is to use the Text Analytics toolbox, which has an HTML parser. It will make it easier to find whatever we are looking in an HTML file.

Misa Taguchi
Misa Taguchi on 23 Jun 2021
Hi. With Google API
key = 'Your_API_Key';
searchengineid = 'Your_Search_Engine_ID';
url = 'https://customsearch.googleapis.com/customsearch/v1';
searchquery = 'Your_Search_Query';
results = webread(url,'q',searchquery,'cx',searchengineid,'key',key);
and see this for more information about the API key and the search engine ID.
After searching, the links are stored in
results.items.link

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!