Save a .txt file

Hey, I need help on the following:
I have a .txt file with many URLs where each line is a diferent URL. Each URL contains only a bunch of text and numbers, nothing else. How do I create a function that reads that same .txt file, accesses each one those URLs and saves to my desktop a .txt that contains the data from that URL. One .txt file per URL.
Thank you for you time.

 採用された回答

Image Analyst
Image Analyst 2012 年 12 月 15 日

1 投票

Did you check the help for fgetl()?
fid = fopen('fgetl.m');
urlString= fgetl(fid);
while ischar(urlString)
disp(urlString)
urlString = fgetl(fid);
webPageContents = urlread(urlString);
% Save it, or whatever.....
end
fclose(fid);

4 件のコメント

Bob Choy
Bob Choy 2012 年 12 月 16 日
編集済み: Bob Choy 2012 年 12 月 16 日
Thank you for your answer, at the moment I have the following:
function save(Name_File,Name_FileLocal);
fid = fopen(Name_File);
while ~feof(fid)
URL = fgetl(fid);
Contents = urlwrite(URL,Name_FileLocal);
end
fclose(fid);
end
Where Name_File is the .txt file with all the urls and Name_FileLocal is the name of the file I want to save the contents to. The problem is it only saves the contents from the first URL in the .txt file.
I need it to read the content from the first URL and save it as content1.txt, read the second URL and save it as content2.txt, and so on.
Any idea on how can I do this?
Thank you.
Image Analyst
Image Analyst 2012 年 12 月 16 日
編集済み: Image Analyst 2012 年 12 月 16 日
You need to create a new filename each iteration, something like this:
fid = fopen(Name_File);
count = 1;
while ~feof(fid)
URL = fgetl(fid);
Name_FileLocal = sprintf('web page %d.txt', count);
count = count+1;
Contents = urlwrite(URL,Name_FileLocal);
end
fclose(fid);
Bob Choy
Bob Choy 2012 年 12 月 16 日
編集済み: Bob Choy 2012 年 12 月 16 日
Thats EXACTLY what I needed. Thank you good sir!
Question answered.
Image Analyst
Image Analyst 2012 年 12 月 16 日
Go ahead and mark it so.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Import and Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by