您的位置:首页 > 编程语言

获得指定网址指定标签内的内容或代码

2010-06-04 20:07 337 查看
uses  msxml;

//从网页源文件中获得指定标签间的代码

function TForm1.GetStr(StrSource, StrBegin, StrEnd: string): string;
var
in_star, in_end: integer;
begin
in_star := AnsiPos(strbegin, strsource) + length(strbegin);
in_end := AnsiPos(strend, strsource);
Result := copy(strsource, in_star, in_end - in_star);
end;

//信息列表点击

procedure TForm1.btn1Click(Sender: TObject);
const
url = 'http://www.xxx.com';
var
req: IXMLHTTPRequest;
StrStream: TStringStream;
SetNoteStr: string;
begin
//利用 IXMLHTTPRequest 获取网页源代码,在调用函数GetStr截取需要的代码
req := CoXMLHTTPRequest.Create;
req.open('Get', url, False, EmptyParam, EmptyParam);
req.send(EmptyParam);     //获取标签直接的代码

SetNoteStr := GetStr(req.responseText, '</script></center>', 'document.body.oncopy=function');       StrStream := TStringStream.Create(SetNoteStr);
try
StrStream.Position := 0;       //直接写入WebBrowser
(WebBrowser.Document as IPersistStreamInit).Load(TStreamadapter.Create(StrStream));
finally
StrStream.Free;      end;
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: