问题描述
使用Indy客户端(IdHTTP),如果我使用以下代码知道文件的实际网址,则可以下载csv文件,该代码非常有效():
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):
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;
是该网站,如果查看页面源代码,我会得到href: cvrda_ratings_2009.csv 。不错,很容易。
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". Nice and easy.
但是
是Stackoverflow上的这个非Delphi示例,,该网站为,如果我按了导出按钮,则可以手动下载csv文件,但可以通过编程方式下载文件,如何获取实际csv文件的完整网址?我在任何地方都找不到它。
Butlooking 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? I can't find it anywhere.
所以我想我的问题是:
是否可以获取正在获取的任何csv文件的整个URL是在TWebBrowser中手动下载的吗?
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?
更新
我希望做什么正在以编程方式下载csv文件。但是我不知道csv文件的网址是什么。如果单击TWebBrowser中的下载按钮以下载csv文件,则会显示一个弹出窗口。然后,我必须在弹出窗口中手动按保存。我希望以编程方式执行此操作。如果知道网址,则可以使用Indy,但是由于我不知道csv文件的网址,因此必须使用TWebBrowser。
What I'm hoping to do is download a csv file programatically. But I don't know what the url of the csv file is. If I click the download button, in TWebBrowser, to download the csv file, a popup appears. 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.
更新(2012年11月12日)
示例2
(此示例在表单上需要一个Tbutton和一个TWebBrowser)
update(12Nov2012)Example 2(This example needs a Tbutton and a TWebBrowser on a Form)
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的答案帮助我了解了很多,它适用于许多网页,但不是全部。我不知道如何使其适用于上述示例2。在上述示例中,可以通过编程方式单击按钮。但在此示例中,要以编程方式下载csv文件,我仍然需要csv文件的url。在这种情况下,有关如何获取csv文件的网址的任何想法。
Sam M's answer helped me understand a lot, and it works for many web pages, but not all. 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. But to download the csv file programatically in this example, I still need the url of the csv file. Any ideas on how to get the url of the csv file in this case.
推荐答案
在网络浏览器获取HTML文档之后,您需要遍历链接标记。根据当前的页面格式,您需要比较每个链接上的innerText,以查看所需的链接。找到所需的链接标记后,获取href属性。如果以某种方式修改了网页,使您正在寻找的链接的innerText被运行网站的人更改了,则此方法将无效。
After the web browser has gotten the HTML document, you need to loop through the link tags. Based on the current page formatting, you would need to compare the innerText on each link to see which one you want. Once you find the desired link tag, get the href property. 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.
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;
如果将来网页开始使用标签ID或标签名称,那就更容易了。使用getElementById函数,然后就不需要遍历所有元素。
If in the future the web page starts using tag ids or tag names, it's even easier. Use the getElementById function and then there's no need to loop through all the elements.
这篇关于在delphi中下载csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!