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

조회 수: 10 (최근 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
Geoff Hayes
Geoff Hayes 2020년 6월 23일
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.

댓글을 달려면 로그인하십시오.

답변 (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.

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

카테고리

Help CenterFile Exchange에서 Web Services에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by