[英]How to find a button with “value” in html page (Webbrowser - Delphi)
[英]How to get the “Text” of a html page ? (Webbrowser - Delphi)
我正在使用WebBrowser來獲取html頁面的來源。 我們的頁面源包含一些文本和一些html標記。 像這樣 :
FONT></P><P align=center><FONT color=#ccffcc size=3>**Hello There , This is a text in our html page** </FONT></P><P align=center> </P>
Html標簽是隨機的,我們無法猜測它們。 那么有沒有辦法只獲取文本並將它們與html標簽分開?
您可以使用TWebBrowser實例來解析並從html代碼中選擇plaint文本。
看這個樣本
uses
MSHTML,
SHDocVw,
ActiveX;
function GetPlainText(Const Html: string): string;
var
DummyWebBrowser: TWebBrowser;
Document : IHtmlDocument2;
DummyVar : Variant;
begin
Result := '';
DummyWebBrowser := TWebBrowser.Create(nil);
try
//open an blank page to create a IHtmlDocument2 instance
DummyWebBrowser.Navigate('about:blank');
Document := DummyWebBrowser.Document as IHtmlDocument2;
if (Assigned(Document)) then //Check the Document
begin
DummyVar := VarArrayCreate([0, 0], varVariant); //Create a variant array to write the html code to the IHtmlDocument2
DummyVar[0] := Html; //assign the html code to the variant array
Document.Write(PSafeArray(TVarData(DummyVar).VArray)); //set the html in the document
Document.Close;
Result :=(Document.body as IHTMLBodyElement).createTextRange.text;//get the plain text
end;
finally
DummyWebBrowser.Free;
end;
end;
您應該看看使用Delphi DOM HTML解析器
如果你的星號是常數,你可以簡單地得到**
之間的所有數字。 如果你的星號不是常數,你可以重寫這個字符串並刪除所有標簽(從<
以>
結尾的東西。或者你可以使用一些DOM解析器庫 。
實質上:一般情況下你不能。
HTML是一種標記語言,具有如此廣泛的用途和令人難以置信的可能性,可以動態地更改內容,實際上幾乎不可能做到這一點(只需看看Web瀏覽器供應商需要多大努力才能通過例如酸測試)。 所以你只能做一個子集。
對於特定且定義良好的HTML子集,您有更好的機會:
首先,您需要在字符串中獲取HTML,然后解析該HTML。
例如,使用Indy可以獲得HTML(請參閱此問題的答案)。
解析高度依賴於您的HTML並且可能非常復雜,您可以嘗試此問題或此搜索 。
您可以像RRuz建議的那樣使用TWebBrowser,但這取決於Internet Explorer。
現代Windows系統不保證再安裝Internet Explorer ......
--jeroen
使用Delphi HTML組件庫只從HTML文檔獲取文本很簡單。 THtDocument.InnerText屬性返回沒有標簽的格式化文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.