2012-02-23 43 views
1

我已经设法让matlab登录到Google趋势来下载csv数据。首先,我使用了DownloadString,然后在使用fastawrite保存csv文件之前将其转换为matlab字符串。调用WebClient.DownloadFile时,Matlab登录和下载失败

但是,即使字符串被正确下载,'\ n'行正在失去一些格式化...这是非常奇怪的,因为如果我通过'\ n'将文件分割成单元格数组,格式化是好的!

所以我现在试图让DownloadFile方法来工作,但我不断收到以下错误:

无方法“DownloadFile”与发现类“System.Net.WebClient”

匹配签名

千恩万谢,

这里的功能:

NET.addAssembly('System.Net'); 

url = 'https://www.google.com/accounts/ClientLogin?accountType=GOOGLE&Email=email&Passwd=pass&service=trendspro&source=test-test-v1'; 
durl = System.String(strcat('http://www.google.com/trends/viz?q=', keyWord, '&date=all&geo=all&graph=all_csv&sort=0&scale=1&sa=N')); 

if exist('googleWebClient','var') 
    client = googleWebClient; 
else 
    client = System.Net.WebClient; 

    response = client.DownloadString(url); 
    sid = char(response.ToString); 
    sid = regexp(sid, '\n', 'split'); sid = sid(1,1); 

    client.Headers.Add('Cookie', char(sid)); 
    assignin('base','googleWebClient',client); 
end 

saveFilePath = System.String(strcat('C:\Dropbox\PROJECTS\', keyWord, '.csv')); 


data = client.DownloadFile(durl, saveFilePath); 

回答

0

什么样的格式正在消失?听起来像你可能只是获得Unix模式(\ n)文本,然后在DOS模式(\ r \ n)编辑器中打开它。快速regexprep(sid, '\r?\n', sprintf('\r\n'))将解决这个问题。切换到DownloadFile可能不会。

就你所得到的“没有这种方法”的错误而言,WebClient.DownloadFile有一个void返回类型。看起来像Matlab的.NET支持认为签名的一部分。删除“数据=”分配,它会消失。

>> client = System.Net.WebClient; 
>> x = client.DownloadFile('http://www.cnn.com', 'C:\temp\blah.html') 
No method 'DownloadFile' with matching signature found for class 'System.Net.WebClient'. 

>> client.DownloadFile('http://www.cnn.com', 'C:\temp\blah.html') 
>> 
+0

非常感谢!我一直在禁止我的头靠在墙上....我删除了data =,它正在下载csv文件。看来,当我使用char(数据)函数从System.String转换为matlab char,然后使用fastawrite进行保存时,它以某种方式损坏了格式。 – user1229124 2012-02-23 21:45:36

+0

对不起,regexprep(sid)格式很好,我使用的是DownloadString而不是DownloadFile,出于某种原因,有一些格式错误进入了csv文件中。 – user1229124 2012-02-23 21:51:22