标签:tor bsp 还原 javascrip pac 人物 警告 启用 option
基本都要放弃了
springmvc 配置了htmlunit之后无法运行,都不能正常实例化webclient,但是突然想起来用maven应用程序测试一下
结果竟然就可以了.好吧,还是有希望的
大佬博客https://www.cnblogs.com/davidwang456/articles/8693050.html
main方法中
public static void main(String[] args) { final WebClient webClient = new WebClient(BrowserVersion.CHROME);//新建一个模拟谷歌Chrome浏览器的浏览器客户端对象 webClient.getOptions().setThrowExceptionOnScriptError(false);//当JS执行出错的时候是否抛出异常, 这里选择不需要 webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);//当HTTP的状态非200时是否抛出异常, 这里选择不需要 webClient.getOptions().setActiveXNative(false); webClient.getOptions().setCssEnabled(false);//是否启用CSS, 因为不需要展现页面, 所以不需要启用 webClient.getOptions().setJavaScriptEnabled(true); //很重要,启用JS webClient.setAjaxController(new NicelyResynchronizingAjaxController());//很重要,设置支持AJAX HtmlPage page = null; try { page = webClient.getPage("http://ent.sina.com.cn/film/");//尝试加载上面图片例子给出的网页 } catch (Exception e) { e.printStackTrace(); } finally { webClient.close(); } webClient.waitForBackgroundJavaScript(30000);//异步JS执行需要耗时,所以这里线程要阻塞30秒,等待异步JS执行结束 String pageXml = page.asXml();//直接将加载完成的页面转换成xml格式的字符串 //TODO 下面的代码就是对字符串的操作了,常规的爬虫操作,用到了比较好用的Jsoup库 Document document = Jsoup.parse(pageXml);//获取html文档 List<Element> infoListEle = document.getElementById("feedCardContent"). getElementsByAttributeValue("class", "feed-card-item");//获取元素节点等 infoListEle.forEach(element -> { System.out.println(element.getElementsByTag("h2").first().getElementsByTag("a").text()); System.out.println(element.getElementsByTag("h2").first().getElementsByTag("a").attr("href")); }); System.out.println("Hello World!");
没写为何这样区
所以我又用f12检查了一下
大概就是这样一个获取路线
输出内容如下
--- exec-maven-plugin:1.2.1:exec (default-cli) @ htmlUnitTest --- 十二月 12, 2018 7:24:32 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:32 下午 com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet isValidCondition 警告: Unhandled CSS condition type ‘PREFIX_ATTRIBUTE_CONDITION‘. Accepting it silently. 十二月 12, 2018 7:24:32 下午 com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError 严重: runtimeError: message=[An invalid or illegal selector was specified (selector: ‘*,:x‘ error: Invalid selector: *:x).] sourceName=[http://n.sinaimg.cn/lib/core/core.js] line=[1] lineSource=[null] lineOffset=[0] 十二月 12, 2018 7:24:32 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:32 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:32 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:32 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:32 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:33 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:33 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:33 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:33 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘text/javascript‘. 十二月 12, 2018 7:24:33 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘text/javascript‘. 十二月 12, 2018 7:24:33 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:33 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:33 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:34 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:34 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:34 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:35 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Expected content type of ‘application/javascript‘ or ‘application/ecmascript‘ for remotely loaded JavaScript element at ‘http://s.weibo.com/ajax/jsonp/suggestion?_cb=jQuery1112023857485591947847_1544613872640&Refer=sina_sug&_=1544613872641‘, but got ‘text/html‘. 十二月 12, 2018 7:24:35 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:35 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:35 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘text/javascript‘. 十二月 12, 2018 7:24:35 下午 org.apache.http.client.protocol.ResponseProcessCookies processCookies 警告: Cookie rejected [U_TRS1="00000097.43c95133.5c10efee.6370decc", version:0, domain:.sina.com.cn, path:/, expiry:Sat Dec 09 19:24:30 CST 2028] Illegal ‘domain‘ attribute ".sina.com.cn". Domain of origin: "widget.weibo.com" 十二月 12, 2018 7:24:35 下午 org.apache.http.client.protocol.ResponseProcessCookies processCookies 警告: Cookie rejected [U_TRS2="00000097.43d75133.5c10efee.9b845144", version:0, domain:.sina.com.cn, path:/, expiry:null] Illegal ‘domain‘ attribute ".sina.com.cn". Domain of origin: "widget.weibo.com" 十二月 12, 2018 7:24:35 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:35 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 十二月 12, 2018 7:24:35 下午 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify 警告: Obsolete content type encountered: ‘application/x-javascript‘. 《狗十三》宣发方呼吁多排片:让电影再多留一会 http://ent.sina.com.cn/m/c/2018-12-12/doc-ihmutuec8610350.shtml 纪录片《二十二》国家公祭日重映 限定南京24影院 http://ent.sina.com.cn/m/c/2018-12-12/doc-ihqackaa6408040.shtml 真人版《刺猬索尼克》电影首曝海报 蓝色轮廓还原 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihqackaa6293876.shtml 犯罪片《公路响马》首曝剧照 科斯特纳合作哈里森 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihmutuec8580858.shtml 美国评论家选择奖公布提名 《宠儿》美国谍梦领跑 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihmutuec8578964.shtml 星二代闪耀!史泰龙女儿与福克斯女儿合作新片 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihmutuec8576402.shtml 钮承泽同学:他最爱抢人女友 涉嫌性侵并不惊讶 http://ent.sina.com.cn/m/c/2018-12-12/doc-ihqackaa6141554.shtml 加奈儿·梦奈加盟传记片《我的人生道路》 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihmutuec8574196.shtml 《东邪西毒》翻拍电视剧?从剧情看跟原小说一致 http://ent.sina.com.cn/m/c/2018-12-12/doc-ihmutuec8571380.shtml 杰克·吉伦哈尔主演丹麦电影《罪人》英文翻拍版 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihmutuec8570538.shtml 托尔·比约恩加盟新片《多哥》 威廉·达福主演 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihmutuec8568073.shtml 朱迪·福斯特执导并主演英文翻拍版《战中女人》 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihmutuec8562053.shtml 哈维尔·巴登与艾丽·范宁合作新片 饰演父女 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihqackaa5931969.shtml 重启版《毒魔复仇》电影运作中 原导演参与制片 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihqackaa5910954.shtml 《奇异博士》导演回归执导续集 康伯巴奇回归主演 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihqackaa5848835.shtml 《“大”人物》曝预告 恶势力“活该挨打”喊出观众心声 http://video.sina.com.cn/p/ent/2018-12-12/detail-ihqackaa5833548.d.html 新版《霹雳娇娃》电影杀青 克里斯汀斯图尔特主演 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihqackaa5801199.shtml 美国非裔影评人协会奖公布 《黑豹》获最佳影片 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihmutuec8543573.shtml 澳洲电影与电视艺术学院奖公布提名 基德曼双提 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihqackaa5684709.shtml DC新片《猛禽小队》再添新反派 扎斯先生将登场 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihqackaa5658377.shtml 《滚石》评选年度电影 《罗马》最佳《毒液》烂 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihqackaa5556399.shtml 北美票房:《无敌破坏王2》三连冠 玛丽女王尚可 http://ent.sina.com.cn/m/f/2018-12-12/doc-ihmutuec8532172.shtml 视频:秦岚谈对宫崎骏印象最深的细节 http://video.sina.com.cn/p/ent/m/c/doc/2018-12-12/152569101314.html 《狗十三》这5年 曹保平焦华静张雪迎的个人故事 http://ent.sina.com.cn/original/qihua/gou13/ 《海王》破8亿曝光“深海惊魂”正片片段 海沟族大战叹为观止 http://k.sina.com.cn/article_1744203407_v67f66e8f00100gx2r.html?from=ent&subch=film 《中国合伙人2》发布“双十二非凡”特辑 非凡网别样促销 http://k.sina.com.cn/article_1338816082_v4fccb65200100dvfp.html?from=ent&subch=film 组图:罗美兰金成均《1988》后再合作 为动画角色配音 9 http://slide.ent.sina.com.cn/film/k/slide_4_704_301475.html 《最萌警探》发布终极预告片,囧囧父子大发神威 http://k.sina.com.cn/article_3561759014_vd44c212600100fv4m.html?from=ent&subch=film 《大黄蜂》发布“蜂芒毕露” 预告 热血奋战肩负保卫地球使命 http://k.sina.com.cn/article_2126150055_v7eba79a700100dsak.html?from=ent&subch=film 贺岁最强喜剧《天气预爆》空降成都肖央常远当众开吃“火锅底料” http://k.sina.com.cn/article_6413792050_17e4ab33200100de0n.html?from=ent&subch=film Hello World! ------------------------------------------------------------------------ BUILD SUCCESS ------------------------------------------------------------------------ Total time: 11.129s Finished at: Wed Dec 12 19:24:37 CST 2018 Final Memory: 7M/150M ------------------------------------------------------------------------
然后大概就是获取了
id叫做的元素,然后再获取节点
然后再输出每个节点的 a标签的文字和href链接
htmlunit第一个爬虫演示 目标网址http://ent.sina.com.cn/film/
标签:tor bsp 还原 javascrip pac 人物 警告 启用 option
原文地址:https://www.cnblogs.com/jnhs/p/10110559.html