獲取含跨域網址的框架網頁的原始碼
前面介紹過的獲取框架網頁的原始碼的方法在針對框架中每個文件的URL都是和主網頁在同一個域名(同一個網站)的情況下是不會出什麼問題的,但如果框架包含的網頁是別的域的話,例如以下網頁:該網頁含左右兩個框架,都包含別的域的網址,該網頁原始碼如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>框架網頁</title>
</head>
<frameset cols="*,*" frameborder="no" border="0" framespacing="0">
<frame src="http://www.baidu.com" name="leftFrame" id="leftFrame" title="leftFrame" />
<frame src="http://www.google.cn" name="mainFrame" id="mainFrame" title="mainFrame" />
</frameset>
<noframes><body>
</body>
</noframes></html>
如果使用以下的程式碼獲取這兩個框架的網頁原始碼:
procedure TForm1.Button1Click(Sender: TObject);
var
doc, framedoc: IHTMLDocument2;
frame_dispatch: IDispatch;
ole_index: OleVariant;
i: Integer;
begin
doc := WebBrowser1.Document as IHTMLDocument2;
if doc = nil then Exit;
for i := 0 to doc.frames.length - 1 do
begin
ole_index := i;
frame_dispatch := doc.frames.item(ole_index);
if frame_dispatch = nil then Continue;
framedoc := (frame_dispatch as IHTMLWindow2).document;
if framedoc = nil then Continue;
ShowMessage(framedoc.body.innerHTML);
end;
end;
執行這段程式碼,系統會彈出“拒絕訪問”的出錯對話方塊,沒有任何商量的餘地。怎麼辦,框架網頁的原始碼還是要獲取的,不會因為跨域問題而放棄的。只有百度一下了,幾番搜尋,終於找到了另一種獲取網頁原始碼的方法,而這種方法可以解決跨域問題,方法如下:
1. 單元引用 Uses MsHtml, ActiveX
2. 程式碼:
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
ole_index: OleVariant;
FrameDis: IDispatch;
FrameWin: IHtmlWindow2;
psi:IServiceProvider;
frameb: IWebBrowser2;
pPersist: IPersistStreamInit;
ss: TStringStream;
str1, str2: String;
begin
if WebBrowser1.Busy then Exit;
Memo1.Lines.Clear;
//獲取主網頁網址
Memo1.Lines.Add(WebBrowser1.OleObject.document.url);
//獲取主網頁原始碼
Memo1.Lines.Add(WebBrowser1.OleObject.document.documentElement.outerHTML);
//新增空行
Memo1.Lines.Add(' ');for i := 0 to WebBrowser1.OleObject.document.frames.length - 1 do
begin
ole_index := i;
FrameDis := (WebBrowser1.Document as IHtmlDocument2).frames.item(ole_index);
FrameDis.QueryInterface(IID_IHTMLWindow2, FrameWin) ;
if FrameWin = nil then Continue;
FrameWin.QueryInterface(IServiceProvider, psi);
if psi = nil then Continue;
psi.QueryService(IID_IWebBrowserApp,IID_IWebBrowser2,frameb);
if frameb=nil then continue;
frameb.Document.QueryInterface(IPersistStreamInit, pPersist);
if pPersist = nil then Continue;
//獲取框架頁網址
Memo1.Lines.Add((frameb.Document as IHtmlDocument2).url);
ss := TStringStream.Create('');
try
//獲取框架頁原始碼
if Succeeded(pPersist.Save(TStreamAdapter.Create(ss), True)) then
begin
str1 := ss.DataString;
str2 := Utf8ToAnsi(str1); //有些網頁使用UTF-8編碼方式,不進行轉換中文會亂碼
if str2 = '' then
Memo1.Lines.Add(str1)
else
Memo1.Lines.Add(str2);
end;
finally
FreeAndNil(ss);
end;
Memo1.Lines.Add(' '); //新增空行
end;
end;
相關文章
- QWebView獲取網頁原始碼WebView網頁原始碼
- Delphi自動提交網頁表單和獲取框架網頁原始碼網頁框架原始碼
- 高效獲取網頁原始碼COM網頁原始碼
- 想獲取JS載入網頁的源網頁的原始碼,不想獲取JS載入後的資料JS網頁原始碼
- 使用WebBrowser控制元件獲取網頁原始碼的方法Web控制元件網頁原始碼
- Delphi獲取所有開啟的IE視窗的網頁原始碼網頁原始碼
- c#利用WebClient和WebRequest獲取網頁原始碼C#Webclient網頁原始碼
- 用python寫個爬取指定網址上所有圖片,並能根據獲取到的圖片網址,進入網址,再次進行圖片獲取 的程式碼指令碼Python指令碼
- javascript獲取網頁的高度JavaScript網頁
- 防止跨域問題無法訪問網址跨域
- 獲取url網址傳遞的指定引數值
- JS獲取跨域的cookie例項JS跨域Cookie
- DukuanCMS_網址導航,導航網站,網址導航原始碼網站原始碼
- js如何獲取網頁的標題JS網頁
- webview獲取網頁titleWebView網頁
- js如何獲取網頁尾本程式碼的內容JS網頁
- 抓取網頁中的原始碼.網頁原始碼
- 如何獲取跨域iframe高度跨域
- 通過網址獲取ES最近10分鐘的資料量
- iOS專案開發實戰——iOS網路程式設計獲取網頁Html原始碼iOS程式設計網頁HTML原始碼
- JavaScript 獲取網頁尾本程式碼內容JavaScript網頁
- iOS專案開發實戰——獲取網頁原始碼的二進位制資料iOS網頁原始碼
- iOS專案開發實戰——使用同步請求獲取網頁原始碼iOS網頁原始碼
- 讓應用程式從IE中獲取鍵入的網址 (轉)
- 防止網頁被嵌入框架的程式碼網頁框架
- php獲取網頁內容的三種方法PHP網頁
- javascript如何獲取當前網頁的url地址JavaScript網頁
- Qt(QtWebEngine)載入本地網頁跨域問題的總結QTWeb網頁跨域
- python3中編碼如何獲取網頁?Python網頁
- js獲取網頁title標題程式碼例項JS網頁
- 兩種方法獲取網頁編碼python版網頁Python
- js/jq 獲取網頁寬高JS網頁
- Python 3.2 獲取中文網頁Python網頁
- 獲取網頁中的密碼和文字輸入框的內容 (轉)網頁密碼
- IOS遍歷網頁獲取網頁中<img>標籤中的圖片urliOS網頁
- 【iOS開發】獲取區域網IPiOS
- JSONP跨域獲取JSON資料(含jQuery方法)——李帥醒部落格JSON跨域jQuery
- javascript獲取滑鼠在網頁文件中的座標JavaScript網頁