Skip to content Skip to sidebar Skip to footer

Matlab Text String/html Parse

I am trying to get information from a website (html) into MATLAB. I am able to get the html from online into a string using: urlread('http://www.websiteNameHere.com...'); Once I h

Solution 1:

Try this and let us know if it works for you -

url_data = urlread('http://www.websiteNameHere.com...');

start_string = '<span class="priceSort">'; %// For your next case, edit this to <span class="milesSort">
stop_string = '</span>';

N1 = numel(start_string);
N2 = numel(stop_string);

start_string_ind = strfind(url_data,start_string);
for count1 = 1:numel(start_string_ind)
    relative_stop_string_ind = strfind(url_data(start_string_ind(count1)+N1:end),stop_string);
    string_found_start_ind = start_string_ind(count1)+N1;
    string_found = url_data(string_found_start_ind:string_found_start_ind+relative_stop_string_ind(1)-2);
    disp(string_found);
end

Solution 2:

Simple solution using strsplit

s = urlread('http://www.websiteNameHere.com...');

x = 'class="priceSort">'; %starting string xy = 'class="milesSort">'; %starting string y
z = '</span>'; %ending string z

s2 = strsplit(s,x); %split for starting string x
s3 = strsplit(s,y); %split for starting string y

result1 = cell(size(s2,2)-1,1); %create cell array 1
result2 = cell(size(s3,2)-1,1); %create cell array 2

%loop through values ignoring first value
%(change ind=2:size(s2,2) to ind=1:size(s2,2) to see why)

%starting string x loop
for ind=2:size(s2,2)
    m = strsplit(s2{1,ind},z);
    result1{ind-1} = m{1,1};
end

%starting string y loop
for ind=2:size(s3,2)
    m = strsplit(s3{1,ind},z);
    result2{ind-1} = m{1,1};
end

Hope this helps

Post a Comment for "Matlab Text String/html Parse"