how to fix index error from urlread

1 回表示 (過去 30 日間)
Abdulrahman Mohamed
Abdulrahman Mohamed 2022 年 10 月 15 日
回答済み: Walter Roberson 2022 年 10 月 16 日
clc
URL = 'http://time.is/';
key1 = 'title="Click for calendar">';
key2 = '</h2>';
data = urlread(URL);
start_ind = strfind(data,key1);
data1 = data(start_ind:end);
off_stop_ind = strfind(data1,key2);
current_date =data(start_ind+numel(key1):start_ind + off_stop_ind(1)-2)
date_split = strsplit(current_date,',')
current_date1 = datestr(strcat(date_split(2),date_split(3)))
I got this error how to fix it
Index exceeds the number of array elements. Index must not exceed 0. Error in date (line 10) current_date =data(start_ind+numel(key1):start_ind + off_stop_ind(1)-2)
  1 件のコメント
per isakson
per isakson 2022 年 10 月 16 日
Check the value of data.

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

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 10 月 16 日
You are using an http url. The result of the query is a 301 "moved permanently". You need to use https and the appropriate reading function.
You might need to provide headers; it gives me a 403 forbidden when I try without headers

カテゴリ

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

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by