này sẽ cho bạn một khởi đầu tốt và ý tưởng sử dụng DOM:
uses
MSHTML,
ActiveX,
ComObj;
procedure DocumentFromString(Document: IHTMLDocument2; const S: WideString);
var
v: OleVariant;
begin
v := VarArrayCreate([0, 0], varVariant);
v[0] := S;
Document.Write(PSafeArray(TVarData(v).VArray));
Document.Close;
end;
function StripMultipleChar(const S: string; const C: Char): string;
begin
Result := S;
while Pos(C + C, Result) <> 0 do
Result := StringReplace(Result, C + C, C, [rfReplaceAll]);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Document: IHTMLDocument2;
Elements: IHTMLElementCollection;
Element: IHTMLElement;
I: Integer;
Line: string;
begin
Document := CreateComObject(CLASS_HTMLDocument) as IHTMLDocument2;
DocumentFromString(Document, '<head>...'); // your HTML here
Elements := Document.all.tags('A') as IHTMLElementCollection;
for I := 0 to Elements.length - 1 do
begin
Element := Elements.item(I, '') as IHTMLElement;
Memo1.Lines.Add('A HREF=' + Element.getAttribute('HREF', 2));
Memo1.Lines.Add('A innerText=' + Element.innerText);
// Text is returned immediately before the element
Line := (Element as IHTMLElement2).getAdjacentText('beforeBegin');
// Line => "Mittwoch, 30. März 2011 12:01 <dir>" OR:
// Line => "Mittwoch, 9. Februar 2005 17:14 113"...
// I don't know what is the actual delimiter:
// It could be [space] or [tab] so we need to normalize the Line
// If it's tabs then it's easier because the timestamps also contains spaces
Line := Trim(Line);
Line := StripMultipleChar(Line, #32); // strip multiple Spaces sequences
Line := StripMultipleChar(Line, #9); // strip multiple Tabs sequences
// TODO: ParseLine (from right to left)
Memo1.Lines.Add(Line);
Memo1.Lines.Add('-------------');
end;
end;
Output:
A HREF=/SubDir/
A innerText=SubDir
Mittwoch, 30. März 2011 12:01 <dir>
-------------
A HREF=/file.txt
A innerText=file.txt
Mittwoch, 9. Februar 2005 17:14 113
-------------
EDIT:
Tôi đã thay đổi StripMultipleChar
triển khai được nhiều hơn đơn giản hóa. nhưng tôi tin rằng phiên bản cũ hơn là được tối ưu hóa để tăng tốc. xem xét thực tế là các dòng có độ dài rất ngắn, sẽ không có nhiều khác biệt về hiệu suất.
Bạn có mẫu không? Không thực sự là một danh sách "danh sách thư mục" tiêu chuẩn (thực ra, có * vài *, tùy thuộc vào máy chủ và hệ điều hành đang chạy) và không biết bạn đang làm việc với điều gì cần làm. –
những gì bạn cần là một trình phân tích cú pháp HTML tạo ra một cây, vì vậy sẽ dễ dàng tìm thấy tất cả các thẻ "A" và hành động theo nhu cầu của bạn. – ComputerSaysNo
Nếu máy chủ hỗ trợ WebDAV, một giải pháp mạnh mẽ hơn sẽ dễ thực hiện với thư viện ứng dụng WebDAV. Mọi cập nhật phiên bản máy chủ hoặc thay đổi phần mềm sẽ phá vỡ trình phân tích cú pháp cụ thể cho đầu ra HTML này. – mjn