高手帮下忙啊~
最近在写一个程序
其中需要实现对HTML代码的解析
现在想到了用IHTMLDocument2
先改变innerHTML
然后读取innerText
但是我写的代码无法通过
附上代码
function GetInnerText(str: string): string;
var
Document: IHTMLDocument2;
begin
IHTMLDocument2(Document).Body.InnerHTML := '<html>' + str + '</html>';//这句运行时报错
Result := IHTMLDocument2(Document).Body.InnerText;
end;
最近在写一个程序
其中需要实现对HTML代码的解析
现在想到了用IHTMLDocument2
先改变innerHTML
然后读取innerText
但是我写的代码无法通过
附上代码
function GetInnerText(str: string): string;
var
Document: IHTMLDocument2;
begin
IHTMLDocument2(Document).Body.InnerHTML := '<html>' + str + '</html>';//这句运行时报错
Result := IHTMLDocument2(Document).Body.InnerText;
end;
还是不行~
都会raise EAccessViolation
如果是要创建一个空的文档~
然后写入内容~
代码该如何写呢?
while webbrowser1.busy do
application.processmessage;(webbrowser1.Document As IHTMLDocument2).Body.InnerHTML := '<html>' + str + '</html>';
MSHTMLprocedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
if (WebBrowser1.Document as IHTMLDocument2) <> nil then
Memo1.Lines.Add((WebBrowser1.Document as IHTMLDocument2).body.innerHTML);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
WebBrowser1.Navigate('Http://www.sina.com.cn');
end;这个是你想要的么?
虽然还没找到可用的代码~
我其实是需要写一个HTML代码解释函数~
取得代码中的文字~
刚开始是通过Delete(str, pos('<', str), pos('>', str));来达到这一目的的~
但Delphi对中文的支持似乎不好~
有中文的情况下总是无法正确的取得文本~
于是就想到用IHTMLDocument2~
这段代码是要独立封装的~
所以不能用WebBrowser.Document
希望有高手能给出正确的代码~
253了~~~
去下载了一个HtmlParser控件~
不用自己写函数了~
楼上的还是都给分~
新来的~
分不多~