MATLAB Answers

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

13 ビュー (過去 30 日間)
Ahd Mukbil
Ahd Mukbil 2020 年 6 月 22 日
回答済み: Misa Taguchi 2021 年 6 月 23 日
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 件のコメント
Ahd Mukbil
Ahd Mukbil 2020 年 6 月 28 日
Thanks Geoff

サインインしてコメントする。

回答 (2 件)

Rafael S.T. Vieira
Rafael S.T. Vieira 2020 年 6 月 23 日
編集済み: Rafael S.T. Vieira 2020 年 6 月 23 日
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.
  1 件のコメント
Ahd Mukbil
Ahd Mukbil 2020 年 6 月 28 日
it worked thank you and yes I understand you.

サインインしてコメントする。


Misa Taguchi
Misa Taguchi 2021 年 6 月 23 日
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!

Translated by