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

26 views (last 30 days)
Ahd Mukbil
Ahd Mukbil on 22 Jun 2020
Answered: Misa Taguchi on 23 Jun 2021
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 = ['' search_target];
%url = ['' search_target];
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?

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:
html = webread(url, 'q', 'mathworks');
tokens = regexp(html , 'href="/url\?q=(?<links>[^ >]+)/', 'names');
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 = '';
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

Community Treasure Hunt

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

Start Hunting!

Translated by