2015-11-03 133 views
-1

我使用Visual Studio創建基本項目,必須啓動Google Chrome瀏覽器,導航到像「google.com」這樣的網站,然後將該網站的源代碼(HTML)轉換爲變量。我怎樣才能做到這一點? 其實,我開始如何獲取Google Chrome URL的HTML?

Process.Start("chrome.exe", "http:\\google.com") 

但是我堅持讓網站的HTML。 有什麼幫助嗎? (請不要告訴我創建一個webbrowser控件,我需要從谷歌Chrome返回HTML而不是通過網頁瀏覽器)

回答

0

您不需要Web瀏覽器控件或啓動過程。剛剛嘗試這一點:

Imports System.Net 

Dim web As New WebClient() 
Dim source As String = web.DownloadString("www.google.com") 
0

下面的代碼將幫助你。它在C#中,但VB可以實現完全相同的事情,只有其他語法。

string urlAddress = "http://google.com"; 

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress); 
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

if (response.StatusCode == HttpStatusCode.OK) 
{ 
using(Stream receiveStream = response.GetResponseStream()) 
{ 
    using(StreamReader readStream = new StreamReader(receiveStream)) 
    { 
    string data = readStream.ReadToEnd(); 
    } 
    }  
} 

附註:我不知道你的要求,我也不想知道,但要知道,這個概念被稱爲屏幕抓取,這是在灰色地帶的IT法律條款。

相關問題