Seeking Assistance in Using a GUI to Return Text From A Webpage

5 vues (au cours des 30 derniers jours)
N/A
N/A le 2 Fév 2021
Commenté : N/A le 10 Fév 2021
Hello!
I am currently developing a MATLAB GUI using App Designer. The premise for this app is that users will able to select 1/5 cities using a dropdown menu. After completing their selection, a button next to the dropdown menu will be coded with a callback function that: (PART 1) returns the html code for the city's 7-day forecast in an editable text field below; and (PART 2) returns the text from the forecast in a multiline text area. See the photos below for reference:
This is the code I have for PART 1 and it seems to work fine:
% Button pushed function: SEARCHWEBButton
function SEARCHWEBButtonPushed(app, event)
app.geosearch = app.CityDropDown.Value;
app.TORn = 'Toronto';
app.OTTn = 'Ottawa';
app.MISSn = 'Mississauga';
app.BRMPn = 'Brampton';
app.HMLTn = 'Hamilton';
app.TORu = 'https://weather.gc.ca/city/pages/on-143_metric_e.html';
app.OTTu = 'https://weather.gc.ca/city/pages/on-118_metric_e.html';
app.MISSu = 'https://weather.gc.ca/city/pages/on-24_metric_e.html';
app.BRMPu = 'https://www.weather.gc.ca/city/pages/on-4_metric_e.html';
app.HMLTu = 'https://weather.gc.ca/city/pages/on-77_metric_e.html';
% Concatenate City Names & URLs
app.CityName = {app.TORn;app.OTTn;app.MISSn;app.BRMPn;app.HMLTn};
app.CityURL = {app.TORu; app.OTTu; app.MISSu; app.BRMPu; app.HMLTu};
%Join Cities and URLS
app.CityLgd = [app.CityName app.CityURL];
%Index Cell Array for Search Term
app.s01 = app.geosearch;
app.s02 = app.CityLgd(:,1);
app.tf = strcmpi(app.s01,app.s02);
app.tf = find(app.tf);
app.index = app.tf;
app.sURL = char(app.CityLgd(app.index,2));
app.WeatherCANLinkEditField.Value = app.sURL;
The following code is what I have for completing PART 2 (returning the text from the weather report):
url = app.sURL;
code = webread(url);
app.RetrievedDataTextArea.Value = extractHTMLText(code);
However, it seems to fail with each trial. Is there a way to code the GUI to return text from an external HTML page?
  2 commentaires
Mario Malic
Mario Malic le 3 Fév 2021
Hello,
One of the Stuart's blog posts https://blogs.mathworks.com/videos/2020/11/13/scraping-links-from-a-list-of-web-pages/ is about extracting links from the web page, which may help you in getting the text.
N/A
N/A le 10 Fév 2021
Modifié(e) : N/A le 10 Fév 2021
Many thanks for the guidance @Mario Malic! I'll be sure to visit the link.

Connectez-vous pour commenter.

Réponse acceptée

Sourabh Kondapaka
Sourabh Kondapaka le 10 Fév 2021
The below code can be used as a reference for extracting the data about temperatures:
url = 'https://weather.gc.ca/city/pages/on-143_metric_e.html';
data = webread(url);
tree = htmlTree(data);
pTags = findElement(tree,'p');
textContent = extractHTMLText(pTags);
As @Mario Malic suggested please go through the blog post.
Documentation links:
Similar questions have been answered in the community:
  1 commentaire
N/A
N/A le 10 Fév 2021
Thank you so much @Sourabh Kondapaka for the assistance! I had been grappling with this challenge for a while and the code you provided offers the exact functionalities I was looking for.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Language Support dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by