Extracting data from a web page

2 次查看(过去 30 天)
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

采纳的回答

Umar
Umar 2024-6-28

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 个评论
Jorge Luis
Jorge Luis 2024-6-28
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 2024-6-28
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by