抓取网页新闻(基本上在互联网上存在了问题是如何把它们整理成你所需要的)
优采云 发布时间: 2021-09-16 08:15抓取网页新闻(基本上在互联网上存在了问题是如何把它们整理成你所需要的)
你想要的任何信息基本上都存在于互联网上。问题是如何将它们组织到您需要的内容中,例如捕获行业网站中所有相关公司的名称、联系电话、电子邮件等,并将它们保存在Excel中进行分析。Web信息捕获变得越来越有用
通常,对于传统web页面,web服务器直接返回HTML。这样的网页很容易掌握。不管使用什么方法,只要获取HTML页面并进行DOM解析即可。但对于需要生成JavaScript的网页来说,这并不容易。张宇没有找到解决这个问题的好办法。欢迎有javascript网页经验的朋友提供建议
所以今天我们将讨论传统HTML网页的信息捕获。虽然如上所述,没有技术难度,但是否有相对简单的方法?使用jQuery和其他JS框架的朋友可能会觉得JavaScript看起来像是获取网页信息的天然助手,它是为网页解析而生的。当然,现在有更多的应用程序,比如服务器端JavaScript应用程序nodejs
如果我们可以使用jQuery在应用程序(如Java程序)中获取web页面,那绝对是令人兴奋的。有现成的解决方案、JavaScript引擎和支持jQuery的环境
工具:Java、rhino、envjs。Rhino是Mozzila提供的开源JavaScript引擎,envjs是一个模拟的浏览器环境,如window。代码如下
package stony.zhang.scrape; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import org.mozilla.javascript.Context; import org.mozilla.javascript.ContextFactory; import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.ScriptableObject; /** * @author MyBeautiful * @Emal: zhangyu0182@sina.com * @date Mar 7, 2012 */ public class RhinoScaper { private String url; private String jsFile; private Context cx; private Scriptable scope; public String getUrl() { return url; } public String getJsFile() { return jsFile; } public void setUrl(String url) { this.url = url; putObject("url", url); } public void setJsFile(String jsFile) { this.jsFile = jsFile; } public void init() { cx = ContextFactory.getGlobal().enterContext(); scope = cx.initStandardObjects(null); cx.setOptimizationLevel(-1); cx.setLanguageVersion(Context.VERSION_1_5); String[] file = { "./lib/env.rhino.1.2.js", "./lib/jquery.js" }; for (String f : file) { evaluateJs(f); } try { ScriptableObject.defineClass(scope, ExtendUtil.class); } catch (IllegalAccessException e1) { e1.printStackTrace(); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (InvocationTargetException e1) { e1.printStackTrace(); } ExtendUtil util = (ExtendUtil) cx.newObject(scope, "util"); scope.put("util", scope, util); } protected void evaluateJs(String f) { try { FileReader in = null; in = new FileReader(f); cx.evaluateReader(scope, in, f, 1, null); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } public void putObject(String name, Object o) { scope.put(name, scope, o); } public void run() { evaluateJs(this.jsFile); } }
测试代码:
Test.js文件,如下所示
$.ajax({ url: "http://www.baidu.com", context: document.body, success: function(data){ // util.log(data); var result =parseHtml(data); var $v= jQuery(result); // util.log(result); $v.find('#u a').each(function(index) { util.log(index + ': ' + $(this).attr("href")); // arr.add($(this).attr("href")); }); } }); function parseHtml(html) { //Create an iFrame object that will be used to render the HTML in order to get the DOM objects //created - this is a far quicker way of achieving the HTML to DOM conversion than trying //to transform the HTML objects one-by-one var oIframe = document.createElement('iframe'); //Hide the iFrame from view oIframe.style.display = 'none'; if (document.body) document.body.appendChild(oIframe); else document.documentElement.appendChild(oIframe); //Open the iFrame DOM object and write in our HTML oIframe.contentDocument.open(); oIframe.contentDocument.write(html); oIframe.contentDocument.close(); //Return the document body object containing the HTML that was just //added to the iFrame as DOM objects var oBody = oIframe.contentDocument.body; //TODO: Remove the iFrame object created to cleanup the DOM return oBody; }
当我们执行单元测试时,我们将在控制台上打印从网页捕获的三个百度连接
0:
1:
2:
测试成功,证明了在Java程序中用jQuery抓取网页是可行的
----------------------------------------------------------------------
张宇,我的好漂亮