.net使用webBrowser爬取頁面標籤內容,簡單爬蟲的幾個關鍵方法
阿新 • • 發佈:2019-02-07
在.net中 可能會碰到需要使用webBrowser進行網頁標籤的操作的情況,下面介紹幾個常用方法,僅供參考
特別說明wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)需要給webBrowser控制元件新增該事件的監聽,用來監聽瀏覽器載入完畢後需要進行的操作
//獲取頁面所有的div元素
HtmlElementCollection hc = this.wb.Document.GetElementsByTagName("div");
//遍歷所有的集合內元素
foreach (HtmlElement el in hc){}
//通過元素屬性進行元素判斷
String cla = el.GetAttribute("className");
if (cla == "item"){}
//獲取a標籤的href屬性
el1.GetAttribute("href");
//獲取img標籤的src屬性
el1.GetAttribute("src");
//獲取標籤內文字
sub_el2.InnerText;
//獲取標籤內html內容
sub_el3.InnerHtml
以上為專案中常用的一些基本方法,如果需要進一步探討,可加我個人微信