Downloading Data off a URL and specifying what portion of the URL to save.

2 ビュー (過去 30 日間)
Christopher Locke
Christopher Locke 2015 年 9 月 14 日
編集済み: Kleber Zuza Nobrega 2016 年 4 月 6 日
I am trying to take Data off 'http://tidesandcurrents.noaa.gov/harcon.html?id=9410170' and upload it to arrays. The problem that I am running into is whenever I websave or webread the URL the entire code comes up. I want to only download and save the Constituent #, Name, Amplitude, Phase, Speed, and Description portion of the URL and have MatLab display those variables separately as arrays, to be used later on.
  2 件のコメント
Kleber Zuza Nobrega
Kleber Zuza Nobrega 2016 年 4 月 6 日
Try this: pattern='<dt>Constituent #</dt>\s*<dd>(?<Cons>.*?(</dd>)?)\s*<dt>Name</dt>\s*<dd>(?<Nam>.*?)</dd>\s*<dt>Amplitude</dt>\s*<dd>(?<Amp>.*?)</dd>\s*<dt>Phase</dt>\s*<dd>(?<Pha>.*?)</dd>\s*<dt>Speed</dt>\s*<dd>(?<Spe>.*?)</dd>\s*<dt>Description</dt>\s*<dd>(?<Des>.*?)</dd>'
res=regexp(code,pattern,'names')
res.Cons %to have access to the text!! and so on res.Nam res.Amp res.Pha res.Spe res.Des
Kleber Zuza Nobrega
Kleber Zuza Nobrega 2016 年 4 月 6 日
編集済み: Kleber Zuza Nobrega 2016 年 4 月 6 日
code=webread('http://tidesandcurrents.noaa.gov/harcon.html?id=9410170')
pattern='<td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td>'
res=regexp(code,pattern,'tokens')
Sorry... Misunderstood. Try this!!

サインインしてコメントする。

回答 (2 件)

Tim Jackman
Tim Jackman 2015 年 9 月 16 日
Webread is designed for RESTful web services, so using it here isn't going to be ideal. You could parse the html code and grab information from the table between the thead and tbody tags, but I'd recommend giving one of these from the File Exchange a try:

Robert Snoeberger
Robert Snoeberger 2015 年 10 月 26 日
編集済み: Robert Snoeberger 2015 年 10 月 26 日
As Tim Jackman correctly pointed out, webread is designed for RESTful web services. Consuming an API provided by a web service will be much easier than screen scraping the data.
The tidesandcurrents.noaa.gov site does provide an API [1], which I found with a google search. The following is an example of using webread to read the data from the web service. The example is based on the "Sample URL requests and responses" section of the API documentation.
>> opts = weboptions('ContentType', 'json');
>> url = 'http://tidesandcurrents.noaa.gov/api/datagetter';
>> result = webread(url, opts, 'product', 'water_level', 'begin_date', '20130101', 'end_date', '20130101', 'station', '8454000', 'time_zone', 'gmt', 'units', 'metric', 'datum', 'mllw', 'format', 'json')
result =
metadata: [1x1 struct]
data: [240x1 struct]
>> result.data(1)
ans =
t: '2013-01-01 00:00'
v: '0.549'
s: '0.004'
f: '0,0,0,0'
q: 'v'
>>

カテゴリ

Help Center および File ExchangeWeb Services についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by