简体   繁体   English

在delphi中下载csv文件

[英]downloading csv files in delphi

Using Indy Clients (IdHTTP) I can download csv files, if I know the actual web address of the file by using the following code, which works perfectly ( original code ):使用 Indy Clients (IdHTTP) 我可以下载 csv 文件,如果我通过使用以下代码知道文件的实际网址,它可以完美运行( 原始代码):

procedure TForm1.Button1Click(Sender: TObject);  
  var  
  Url, LocalFile: String;  
  Strm: TFileStream;  
begin  
    Url := 'http://www.cvrda.org/boats/cvrda_handicap/cvrda_ratings_2009.csv';  
    LocalFile := 'C:\cvrda_ratings_2009.csv';  
    Strm := TFileStream.Create(LocalFile, fmCreate);  

    try  
      try  
        IdHTTP1.Get(Url, Strm);  
      finally  
        Strm.Free;  
      end;  
   except  
     DeleteFile(LocalFile);  
     raise;  
   end;  
end; 

http://www.cvrda.org/boats/cvrda_handicap/cvrda_handicap.htm is the web site and if I look at page source I get the href:"cvrda_ratings_2009.csv".http://www.cvrda.org/boats/cvrda_handicap/cvrda_handicap.htm是网站,如果我查看页面源代码,我会得到 href:"cvrda_ratings_2009.csv"。 Nice and easy.好,易于。

But looking at this non-Delphi example from Stackoverflow, example here , the website is here , if I press the export button, I can download the csv file manually, but to programaticaly download the file, how do I get the whole url of the actual csv file?但是从 Stackoverflow 看这个非 Delphi 例子, 例子在这里,网站在这里,如果我按下导出按钮,我可以手动下载 csv 文件,但是要以编程方式下载文件,我如何获取整个 url实际的csv文件? I can't find it anywhere.我在任何地方都找不到。

So I guess my question is: Is there a way to get the whole url of any csv file that is being downloaded manually in TWebBrowser?所以我想我的问题是:有没有办法获取在 TWebBrowser 中手动下载的任何 csv 文件的整个 url?

update更新

What I'm hoping to do is download a csv file programatically.我希望做的是以编程方式下载 csv 文件。 But I don't know what the url of the csv file is.但我不知道 csv 文件的 url 是什么。 If I click the download button, in TWebBrowser, to download the csv file, a popup appears.如果我在 TWebBrowser 中单击下载按钮来下载 csv 文件,则会出现一个弹出窗口。 I then have to manually press 'save' in the popup.然后我必须在弹出窗口中手动按“保存”。 I hoping to do this programatically.我希望以编程方式做到这一点。 If I know the url, I can use Indy, but because I don't know the url of the csv file, I have to use TWebBrowser.如果我知道url,我可以使用Indy,但是因为我不知道csv文件的url,我必须使用TWebBrowser。

update(12Nov2012) Example 2 (This example needs a Tbutton and a TWebBrowser on a Form) update(12Nov2012)示例 2(此示例需要一个 Tbutton 和一个表单上的 TWebBrowser)

procedure TForm1.Button1Click(Sender: TObject);
var
  ovLinks: OleVariant;
  x:integer;
begin
  WebBrowser1.navigate('http://financials.morningstar.com/income-statement/is.html?t=AAPL&ops=clear');
  //wait for page to load
  ovLinks := WebBrowser1.OleObject.Document.all.tags('A');
  if ovLinks.Length > 0 then
  begin
    for x := 0 to ovLinks.Length-1 do
    begin
    if Pos('javascript:SRT_stocFund.Export()', ovLinks.Item(x).href) > 0 then
      begin
        ovLinks.Item(x).click;
        Break;
      end;
    end;
  end;
end;

Sam M's answer helped me understand a lot, and it works for many web pages, but not all. Sam M 的回答帮助我理解了很多,它适用于许多网页,但不是全部。 I have no idea how to make it work for the above Example 2. In the above example I can download the csv file manually, after programatically clicking the 'Export' button.我不知道如何使它适用于上面的示例 2。在上面的示例中,我可以在以编程方式单击“导出”按钮后手动下载 csv 文件。 But to download the csv file programatically in this example, I still need the url of the csv file.但是要在本例中以编程方式下载 csv 文件,我仍然需要 csv 文件的 url。 Any ideas on how to get the url of the csv file in this case.在这种情况下如何获取 csv 文件的 url 的任何想法。

After the web browser has gotten the HTML document, you need to loop through the link tags.在 Web 浏览器获取 HTML 文档后,您需要遍历链接标签。 Based on the current page formatting, you would need to compare the innerText on each link to see which one you want.根据当前的页面格式,您需要比较每个链接上的 innerText 以查看您想要哪个链接。 Once you find the desired link tag, get the href property.找到所需的链接标签后,获取 href 属性。 This will not work if the web page is modified in such a way that the innerText of the link you are looking for is changed by the people who run the web site.如果网页的修改方式使您正在查找的链接的 innerText 被运行该网站的人更改,这将不起作用。

procedure Parse;
var URL : string;
    i: integer;
    Document: variant;
begin
  Document := WebBrowser.Document AS IHTMLDocument3;
  for i := 0 to Document.Links.Length - 1 do begin
    if Document.Links.Item(i).innerText = 'here' then begin
      URL := Document.Links.Item(i).href;
      Break;
    end;
  end;
end;

If in the future the web page starts using tag ids or tag names, it's even easier.如果将来网页开始使用标签 ID 或标签名称,那就更容易了。 Use the getElementById function and then there's no need to loop through all the elements.使用 getElementById 函数,则无需遍历所有元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM