源代碼網(wǎng)站 C#winform如何獲取網(wǎng)頁(yè)源碼中的數(shù)據(jù)?
C#winform如何獲取網(wǎng)頁(yè)源碼中的數(shù)據(jù)?private void button1單擊(object sender,EventArgs e){try{if(此.txtUrl.Text. 修剪()。長(zhǎng)
C#winform如何獲取網(wǎng)頁(yè)源碼中的數(shù)據(jù)?
private void button1單擊(object sender,EventArgs e)
{
try
{
if(此.txtUrl.Text. 修剪()。長(zhǎng)度==0)
{
(”請(qǐng)輸入主入口地址!“)]}
else
{
//在此處獲取getwebcontent方法的結(jié)果
string webcontent=getwebcontent(此.txtUrl.Text. trim())]//聲明一個(gè)WebBrowser
WebBrowser WebBrowser=new WebBrowser()webBrowser.導(dǎo)航(" 關(guān)于:空白“”
//將getwebcontent方法返回的結(jié)果轉(zhuǎn)換成htmldocument,即可正確處理網(wǎng)頁(yè)中的元素。
HtmlDocument htmlDoc=webBrowser.Document.OpenNew(正確)htmlDoc.寫(xiě)入(webcontent)
//獲取網(wǎng)頁(yè)正文中的HTML代碼
字符串outerhtml=htmlDoc.Body.OuterHtml
//獲取頁(yè)面標(biāo)題
字符串outertitle=htmlDoc.標(biāo)題
this.txtDocumentTitle.Text文件=外層this.txtDocumentConent.Text文件=outerHtml;
}
}
catch(Exception ex)
{MessageBox.顯示( 例如消息)
}
}