国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
[C#]兩個(gè)分析HTML網(wǎng)頁(yè)的方法[轉(zhuǎn)]

[C#]兩個(gè)分析HTML網(wǎng)頁(yè)的方法[轉(zhuǎn)]

原文見:http://blog.joycode.com/mvm/archive/2004/04/27/20352.aspx

有人想把Web Page拉下來(lái)并抽取其中的內(nèi)容。這其實(shí)是搜索引擎的一項(xiàng)最最基本的工作:下載,抽取,再下載。我早年做過一個(gè)Search Engine項(xiàng)目,不過代碼都已經(jīng)不見了。這次有人又問到我這個(gè)事情,我給攢了兩個(gè)方法。

方法A,在一個(gè)WinForm里面用一個(gè)隱藏的Browser控件下載Web Page,并用IHTMLDocument來(lái)分析內(nèi)容。這個(gè)方法比較簡(jiǎn)單,但如果對(duì)于大量文件的分析速度很慢。

這個(gè)方法中用到的主要代碼如下:
private void button1_Click(object sender, System.EventArgs e) {
object url="http://www.google.com";
object nothing=null;
this.axWebBrowser1.Navigate2(ref url,ref nothing,ref nothing,ref nothing,ref nothing);
this.axWebBrowser1.DownloadComplete+=new System.EventHandler(this.button2_Click);
}

private void button2_Click(object sender, System.EventArgs e) {
this.textBox1.Text="";
mshtml.IHTMLDocument2 doc=(mshtml.IHTMLDocument2)this.axWebBrowser1.Document;
mshtml.IHTMLElementCollection all=doc.all;
System.Collections.IEnumerator enumerator=all.GetEnumerator();
while(enumerator.MoveNext() && enumerator.Current!=null)
{
mshtml.IHTMLElement element=(mshtml.IHTMLElement)(enumerator.Current);
if(this.checkBox1.Checked==true)
{
this.textBox1.Text+="\r\n\r\n"+element.innerHTML;
}
else
{
this.textBox1.Text+="\r\n\r\n"+element.outerHTML;
}
}
}

方法B,用System.Net.WebClient下載Web Page存到本地文件或者String中用正則表達(dá)式來(lái)分析。這個(gè)方法可以用在Web Crawler等需要分析很多Web Page的應(yīng)用中。

下面是一個(gè)例子,能夠把http://www.google.com首頁(yè)里的所有的Hyperlink都抽取出來(lái):

using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace HttpGet{
class Class1{
[STAThread]
static void Main(string[] args){
System.Net.WebClient client=new WebClient();
byte[] page=client.DownloadData("http://www.google.com");
string content=System.Text.Encoding.UTF8.GetString(page);
stringregex="href=[\\\"\\\‘](http:\\/\\/|\\.\\/|\\/)?\\w+(\\.\\w+)*(\\/\\w+(\\.\\w+)?)*(\\/|\\?\\w*=\\w*(&\\w*=\\w*)*)?[\\\"\\\‘]";
Regex re=new Regex(regex);
MatchCollection matches=re.Matches(content);

System.Collections.IEnumerator enu=matches.GetEnumerator();
while(enu.MoveNext() && enu.Current!=null)
{
Match match=(Match)(enu.Current);
Console.Write(match.Value+"\r\n");
}
}
}
}

真正做爬蟲的,都是用正則表達(dá)式來(lái)做抽取的,可以找些開源的爬蟲,代碼都差不多。只是有些更高,可以把Flash或者JavaScript里面的URL都抽取出來(lái)。

再補(bǔ)充一個(gè),有人問我如果一個(gè)element是用document.write畫出來(lái)的,還能不能在DOM里面取到。答案是肯定的。具體取的方法也和平常的一樣。下面這個(gè)HTML就演示了從DOM里面取到用document.write動(dòng)態(tài)生成的HTML Tag:

<FORM>
<SCRIPT>
document.write("<input type=button id=‘btn1‘ value=‘button 1‘>");
</SCRIPT>
<INPUT onclick=show() type=button value="click me">
</FORM>
<TEXTAREA id=allnode rows=29 cols=53></TEXTAREA>
<SCRIPT>
function show()
{
document.all.item("allnode").innerText="";
var i=0;
for(i=0;i<document.forms[0].childNodes.length;i++)
{
 document.all.item("allnode").innerText=document.all.item("allnode").innerText+"\r\n"+document.forms[0].childNodes.tagName+""+document.forms[0].childNodes.value;
}
}
</SCRIPT>
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
ASP獲取客戶端硬件信息(CPU、硬盤、主板、mac地址等)
WPF 與網(wǎng)頁(yè)交互(Frame,WebBrowser)(
MOX.cc [C#:在WebBrowser控件中阻止alert[警告框],confirm...
利用webBrowser實(shí)現(xiàn)自動(dòng)提交頁(yè)面的方法
Javascript+DOM訪問XML文件中的數(shù)據(jù)
C#的WebBrowser操作frame
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服