public string GetMeta(string name) { var cc = hostBrowser.GetElementsByTagName(true, "META"); mshtml.IHTMLElement el = cc.item(name, 0) as IHTMLElement; return(el.getAttribute("content") as string); }
private bool GotoNextPage(WebBrowser wb, bool isLastPage) { mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)wb.Document; // string html = doc2.body.innerHTML; var currentUrl = wb.Source.AbsoluteUri; currentUrl = currentUrl.Replace("#", ""); if (currentUrl == this.TargetUri) { mshtml.IHTMLElement example1_next = (mshtml.IHTMLElement)doc2.all.item("example1_next", 0); example1_next.click(); int pageCounter = 0; var pagenumStr = example1_next.getAttribute("data-dt-idx"); if (!int.TryParse(pagenumStr.ToString(), out pageCounter)) { MessageBox.Show(string.Format("翻页失败,请手工执行")); } //边界条件 var classstr = example1_next.getAttribute("class"); if (classstr == null) { MessageBox.Show(string.Format("example1_next miss {0} attr,请手工点击[下一页]", "class")); } else { if (classstr.ToString().Contains("disabled")) { isLastPage = true; } } // class = paginate_button current 定 } return(isLastPage); }
//空间动态里的好友 private void button9_Click(object sender, EventArgs e) { string info = ""; mshtml.HTMLDocumentClass htmlDoc = webBrowser1.Document.DomDocument as mshtml.HTMLDocumentClass; for (int i = 0; i < htmlDoc.frames.length; ++i) { object index = i; mshtml.IHTMLWindow2 frameWindow = htmlDoc.frames.item(ref index) as mshtml.IHTMLWindow2; IHTMLDocument2 frame = CodecentrixSample.CrossFrameIE.GetDocumentFromWindow(frameWindow); for (int j = 0; j < frame.frames.length; ++j) { try { object rj = j; mshtml.IHTMLWindow2 frameWindow2 = frame.frames.item(ref rj) as mshtml.IHTMLWindow2; IHTMLDocument2 frame2 = CodecentrixSample.CrossFrameIE.GetDocumentFromWindow(frameWindow2); mshtml.IHTMLElementCollection userlist = (mshtml.IHTMLElementCollection)frame2.all.tags("div"); IEnumerator users = userlist.GetEnumerator(); while (users.MoveNext()) { mshtml.IHTMLElement element = (mshtml.IHTMLElement)users.Current; if (element.className == "user-list" || element.className == "ui-avatar") { mshtml.IHTMLElementCollection childrenCol = (mshtml.IHTMLElementCollection)element.children; IEnumerator cusers = childrenCol.GetEnumerator(); while (cusers.MoveNext()) { mshtml.IHTMLElement celement = (mshtml.IHTMLElement)cusers.Current; string href = (string)celement.getAttribute("href", 0); if (href.Contains("http:")) { info += href + "\n"; } } } } } catch (Exception) { } } } MessageBox.Show(info); }
private FindAttribute GetCssPathFinder(IHTMLElement element) { FindAttribute Finder = Finder = new FindAttribute(); Finder.FindMethod = FindMethods.CssSelector; string selector = element.getAttribute("__selector", 0) as string; if (!string.IsNullOrEmpty(selector)) { Finder.FindValue = selector; } else { Finder.FindValue = this.AppContext.Browser.Selector.GetCssPath(element, false); } return(Finder); }
//检测是否已评论 private mshtml.IHTMLElement gerCommented() { String myqq = getMyQQ(); mshtml.HTMLDocumentClass htmlDoc = webBrowser1.Document.DomDocument as mshtml.HTMLDocumentClass; for (int i = 0; i < htmlDoc.frames.length; ++i) { object index = i; mshtml.IHTMLWindow2 frameWindow = htmlDoc.frames.item(ref index) as mshtml.IHTMLWindow2; IHTMLDocument2 frame = CodecentrixSample.CrossFrameIE.GetDocumentFromWindow(frameWindow); mshtml.IHTMLElementCollection tagls = (mshtml.IHTMLElementCollection)frame.all.tags("div"); IEnumerator tagie = tagls.GetEnumerator(); while (tagie.MoveNext()) { mshtml.IHTMLElement element = (mshtml.IHTMLElement)tagie.Current; if (element.className == "mod-comments") { mshtml.IHTMLElementCollection childrenCol = (mshtml.IHTMLElementCollection)element.all; IEnumerator cusers = childrenCol.GetEnumerator(); bool cangonext = true; while (cusers.MoveNext()) { mshtml.IHTMLElement celement = (mshtml.IHTMLElement)cusers.Current; Console.WriteLine(celement.className); if (celement.className == "comments-item bor3") { if ((String)celement.getAttribute("data-uin", 0) == myqq) { cangonext = false; break; } } } //1.打开编辑框 if (cangonext)//自己没有评论 { return(element); } } } } return(null); }
public static bool IsElementMatch(mshtml.IHTMLElement ele, ElementTag tag, string itemName, string keyword = "") { itemName = itemName.Trim(); if (tag == ElementTag.ID) { return(!string.IsNullOrEmpty(ele.id) && ele.id.Trim().Equals(itemName)); } if (tag == ElementTag.name) { return((ele.getAttribute("name", 0) != null) && ele.getAttribute("name", 0).ToString().Trim().Equals(itemName)); } if (tag == ElementTag.outerText) { return(!string.IsNullOrEmpty(ele.outerText) && (ele.outerText.Trim().IndexOf(itemName) != -1)); } if (tag == ElementTag.className) { return(!string.IsNullOrEmpty(ele.className) && ele.className.Trim().Equals(itemName)); } if (tag == ElementTag.outerHTML) { return(!string.IsNullOrEmpty(ele.outerHTML) && ele.outerHTML.Trim().Equals(itemName)); } if (tag == ElementTag.value) { return((ele.getAttribute("value", 0) != null) && ele.getAttribute("value", 0).ToString().Trim().Equals(itemName)); } if (tag == ElementTag.href) { return((ele.getAttribute("href", 0) != null) && ele.getAttribute("href", 0).ToString().Trim().Equals(itemName)); } if (tag != ElementTag.src) { return(false); } if (ele.getAttribute("src", 0) == null) { return(false); } return(ele.getAttribute("src", 0).ToString().Trim().Equals(itemName) || (!string.IsNullOrEmpty(keyword) && (ele.getAttribute("src", 0).ToString().Trim().IndexOf(keyword) != -1))); }
//获取【说说】的好友信息 private void button7_Click(object sender, EventArgs e) { string info = null; mshtml.HTMLDocumentClass htmlDoc = webBrowser1.Document.DomDocument as mshtml.HTMLDocumentClass; for (int i = 0; i < htmlDoc.frames.length; ++i) { object index = i; mshtml.IHTMLWindow2 frameWindow = htmlDoc.frames.item(ref index) as mshtml.IHTMLWindow2; IHTMLDocument2 frame = CodecentrixSample.CrossFrameIE.GetDocumentFromWindow(frameWindow); mshtml.IHTMLElementCollection divs = (mshtml.IHTMLElementCollection)frame.all.tags("div"); IEnumerator div = divs.GetEnumerator(); while (div.MoveNext()) { mshtml.IHTMLElement item = (mshtml.IHTMLElement)div.Current; if (item.className == "ui_avatar" || item.className == "feed_like") { mshtml.IHTMLElementCollection childrenCol = (mshtml.IHTMLElementCollection)item.children; IEnumerator cusers = childrenCol.GetEnumerator(); while (cusers.MoveNext()) { mshtml.IHTMLElement celement = (mshtml.IHTMLElement)cusers.Current; if (celement.tagName == "a" || celement.tagName == "A") { string href = (string)celement.getAttribute("href", 0); if (href != null && href.Contains("http:")) { info += href + "\n"; } } } } } } MessageBox.Show(info); }
static public string ActiveElementAttribute(IHTMLElement element, string AttributeName) { if (element == null) { return(""); } string strValue = ""; try { strValue = element.getAttribute(AttributeName, 0) as string ?? ""; if (string.IsNullOrEmpty(strValue)) { strValue = element.innerText; } } catch (Exception) { } return(strValue); }
public static bool IsElementMatchType(mshtml.IHTMLElement ele, string typeName) { return((ele.getAttribute("type", 0) != null) && ele.getAttribute("type", 0).ToString().ToLower().Trim().Equals(typeName)); }
public void StartCrawl(object view, Button btn)// private void BtnDownload_Click(object sender, RoutedEventArgs e) { bool IsUnConfirmChecked = true; WebBrowser wb = null; var vw = view as WebBoxView; if (vw != null) { wb = vw.FindName("webBox") as WebBrowser; Uri uri = new Uri(this.TargetUri); if (wb == null || wb.Source != uri) { MessageBox.Show("没有适合的数据"); return; } } #region 确认标志 IsUnConfirmChecked (qrzt0~未确认 / qrzt1~已确认) var currentUrl = wb.Source.AbsoluteUri; if (currentUrl.Contains("#")) { currentUrl = currentUrl.Replace("#", ""); } if (currentUrl == this.TargetUri) { mshtml.IHTMLDocument2 doc2 = (mshtml.IHTMLDocument2)wb.Document; mshtml.IHTMLElement qrzt0 = (mshtml.IHTMLElement)doc2.all.item("qrzt0", 0); if (qrzt0 != null) { IsUnConfirmChecked = (bool)qrzt0.getAttribute("CHECKED"); // login_header.setAttribute("style", "display: none;"); Debug.WriteLine(IsUnConfirmChecked); } } #endregion bool isOffline = string.Compare(this.IsOffline, "1", StringComparison.InvariantCultureIgnoreCase) == 0 ? true : false; #region Auto pagenate : id = example1_next this.IsBusy = true; this.BusyText = "正在同步数据,请稍后......"; wb.Visibility = Visibility.Hidden; ProgressBar pBar = vw.FindName("ProgBar") as ProgressBar; if (pBar != null) { ConfigProgressBar(pBar); } //下一页定总页数和边界条件 var isLastPage = false; var pNum = 1; bool hasValidData = true; do { #region Crawl Current Page //法1:<N>基于Httphelper,这样下载会要求程序自己实现验证授权 //HttpHelper httpHelper = new HttpHelper(); //HttpItem rq = new HttpItem(); //rq.URL = uri.AbsoluteUri; //HttpResult html = httpHelper.GetHtml(rq); //Debug.WriteLine(html.Html); //法2:直接基于WebBrowser,授权是由用户手动实现的 CrawlCurrentPage(wb, isOffline, IsUnConfirmChecked, ref hasValidData); #endregion if (isOffline) { MessageBox.Show("离线测试结束"); return; } if (hasValidData) { this.IsBusy = true; this.BusyText = string.Format("已同步第{0}页数据", (pNum++).ToString()); isLastPage = GotoNextPage(wb, isLastPage); } else { this.IsBusy = false; wb.Visibility = Visibility.Visible; isLastPage = true; } }while (!isLastPage); #endregion if (isLastPage && hasValidData) { this.IsBusy = false; wb.Visibility = Visibility.Visible; MessageBox.Show("所有数据已同步完成"); } }
public override void Tick() { if (Enable) { try { string info = null; mshtml.HTMLDocumentClass htmlDoc = webBrowser1.Document.DomDocument as mshtml.HTMLDocumentClass; for (int i = 0; i < htmlDoc.frames.length; ++i) { object index = i; mshtml.IHTMLWindow2 frameWindow = htmlDoc.frames.item(ref index) as mshtml.IHTMLWindow2; IHTMLDocument2 frame = CodecentrixSample.CrossFrameIE.GetDocumentFromWindow(frameWindow); mshtml.IHTMLElementCollection divs = (mshtml.IHTMLElementCollection)frame.all.tags("div"); IEnumerator div = divs.GetEnumerator(); while (div.MoveNext()) { mshtml.IHTMLElement item = (mshtml.IHTMLElement)div.Current; if (item.className == "ui_avatar" || item.className == "feed_like") { mshtml.IHTMLElementCollection childrenCol = (mshtml.IHTMLElementCollection)item.children; IEnumerator cusers = childrenCol.GetEnumerator(); while (cusers.MoveNext()) { mshtml.IHTMLElement celement = (mshtml.IHTMLElement)cusers.Current; if (celement.tagName == "a" || celement.tagName == "A") { string href = (string)celement.getAttribute("href", 0); if (href != null && href.Contains("http:")) { info += href + "\n"; spider.AddQQ(href); } } } } } } //string info = ""; //for (int i = 0; i < webBrowser1.Document.Window.Frames.Count; ++i) //{ // HtmlDocument frame = webBrowser1.Document.Window.Frames[i].Document; // HtmlElementCollection divs = frame.GetElementsByTagName("div"); // for (int j = 0; j < divs.Count; ++j) // { // string className = divs[j].GetAttribute("className"); // if (className == "ui_avatar") // { // HtmlElementCollection a = divs[j].GetElementsByTagName("a"); // if (a != null && a.Count > 0) // { // string url = a[0].GetAttribute("href"); // info += url + "\n"; // spider.AddQQ(url); // } // } // else if (className == "feed_like") // { // HtmlElementCollection a = divs[j].GetElementsByTagName("a"); // if (a != null && a.Count > 0) // { // for (int k = 0; k < a.Count; ++k) // { // if (a[k].GetAttribute("className") == "c_tx") // { // string url = a[0].GetAttribute("href"); // info += url + "\n"; // spider.AddQQ(url); // } // } // } // } // } //} spider.UserGetOver(); Log(info); ChangeCommon("NewUser"); } catch (Exception e) { Log(e.Message); ChangeCommon("NewUser"); } } }
void IDocHostUIHandler.TranslateAccelerator(ref tagMSG lpmsg, ref Guid pguidCmdGroup, uint nCmdID) { const int Ok = 0; const int Error = 1; const int VK_BACK = 0x08; const int WM_KEYDOWN = 0x0100; const int WM_KEYUP = 0x0101; const int VK_CONTROL = 0x11; const int VK_ALT = 0x12; if (lpmsg.message != WM_KEYUP) { if (GetAsyncKeyState(VK_BACK) < 0) { mshtml.IHTMLDocument2 htmlDoc = axWebBrowser1.Document as mshtml.IHTMLDocument2; mshtml.IHTMLElement htmlElement = htmlDoc.activeElement as mshtml.IHTMLElement; string activeTag = htmlElement.tagName.ToLower(); if (activeTag == "a") { throw new COMException("", Ok); // returns HRESULT = S_OK } } } if (lpmsg.message != WM_KEYDOWN) { // allow message throw new COMException("", Error); // returns HRESULT = S_FALSE } if (GetAsyncKeyState(VK_CONTROL) < 0) //CONTROL KEY PRESSED { // disable all control keys except Ctrl-A Ctrl-C and Ctrl-V lpmsg.lParam &= 0xFF; // get the virtual keycode if ((lpmsg.lParam != 'A') || ((lpmsg.lParam != 'C')) || ((lpmsg.lParam != 'V'))) { throw new COMException("", Ok); // returns HRESULT = S_OK } } else if (GetAsyncKeyState(VK_ALT) < 0) //ALT KEY PRESSED { //disable all Alt key actions throw new COMException("", Ok); // returns HRESULT = S_OK } else { // Ctrl key not pressed: allow message mshtml.IHTMLDocument2 htmlDoc = axWebBrowser1.Document as mshtml.IHTMLDocument2; mshtml.IHTMLElement htmlElement = htmlDoc.activeElement as mshtml.IHTMLElement; string activeTag = htmlElement.tagName.ToLower(); string activeElementType = htmlElement.getAttribute("type", 0).ToString(); if (GetAsyncKeyState(VK_BACK) < 0) { if ((activeTag == "input" && activeElementType == "text") || activeTag == "textarea" || activeTag == "text" || activeTag == "password") { throw new COMException("", Error); // returns HRESULT = S_FALSE } else { throw new COMException("", Ok); // returns HRESULT = S_OK } } } // allow everything else throw new COMException("", Error); // returns HRESULT = S_FALSE }