Clear Filters
Clear Filters

Extracting data from a web page

22 views (last 30 days)
Jorge Luis
Jorge Luis on 28 Jun 2024 at 14:07
Commented: Umar on 28 Jun 2024 at 17:45
Hi, I would like to automatize the extraction of data from different events and I would like some help on how extract the numerical data from this webpage: M 7.2 - 8 km W of Atiquipa, Peru (usgs.gov) like these:
W-phase Moment Tensor (Mww)
Moment:6.955e+19 N-m
Magnitude:7.16 Mww
Depth:17.5 km
Percent DC:96%
Half Duration:8.50 s
Catalog:US
Data Source:US 3
Contributor:US 3
Nodal Planes
NP1
Strike:309°
Dip: 16°
Rake: 57°
NP2
Strike: 164°
Dip:77°
Rake:99°
Principal AxesAxisValuePlungeAzimuthT6.893e+1957°86°N0.123e+199°342°P-7.016e+1931°246°
Principal Axes
T:6.893e+19,57°,86°
N:0.123e+19,9°,342°
P:-7.016e+19,31°,246°
I would appreciate the help

Accepted Answer

Umar
Umar on 28 Jun 2024 at 16:22

Hi Jorge,

You can use Matlab's web scraping capabilities along with regular expressions to extract the desired numerical data. Here is a sample code snippet that demonstrates how to extract the Moment value from the provided webpage:

% URL of the webpage

url = 'https://earthquake.usgs.gov/earthquakes/eventpage/us7000e7y0/executive';

% Read the webpage content

webpage = webread(url);

% Extract Moment value using regular expressions

moment_pattern = 'Moment:(\d+\.\d+e[+-]\d+) N-m';

moment_match = regexp(webpage, moment_pattern, 'tokens', 'once');

% Display the extracted Moment value

if ~isempty(moment_match)

    moment_value = str2double(moment_match{1});
    disp(['Extracted Moment Value: ', num2str(moment_value)]);

else

    disp('Moment value not found.');

end

Hope that answers your question.

  2 Comments
Jorge Luis
Jorge Luis on 28 Jun 2024 at 17:13
Hi, thank you for your response. The problem is that the variables are not captured as they are when using the webread function.
Umar
Umar on 28 Jun 2024 at 17:45
Hi Jorge,
When dealing with variables not being captured correctly in the webread function in Matlab, ensure that the URL is properly formatted and that the variables are appropriately encoded. You can use the weboptions function to set parameters like 'MediaType' and 'RequestMethod' to handle different types of data. Additionally, consider encoding the variables using functions like urlencode to prevent any issues with special characters. By adjusting these settings and encoding the variables correctly, you can enhance the accuracy of capturing variables when using the webread function in Matlab.
Let me know if you need further assistance, I will be happy to help.

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!