Extracting data from a web page

조회 수: 3 (최근 30일)
Jorge Luis
Jorge Luis 2024년 6월 28일
댓글: Umar 2024년 6월 28일
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