我如何使用JavaScript抓取HTML代码?

时间:2017-04-05 05:48:26

标签: javascript jquery html html-content-extraction

我正在尝试创建一个客户端端刮刀。我想只使用将在客户端运行的javascript或jQuery,并以JSON格式获取html输出并将其显示在我的网页上。
这是我试过的:

<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
   var ExternalURL = "www.example.com"; // This address must not contain any leading "http://"
   var ContentLocationInDOM = "#someNode > .childNode"; // If you’re trying to get sub-content from the page, specify the "CSS style" jQuery syntax here, otherwise set this to "null"

   $(document).ready(loadContent);
   function loadContent()
   {
      var QueryURL = "http://anyorigin.com/get?url=" + ExternalURL + "&callback=?";
      $.getJSON(QueryURL, function(data){
         if (data && data != null && typeof data == "object" && data.contents && data.contents != null && typeof data.contents == "string")
         {
            data = data.contents.replace(/<script[^>]*>[sS]*?</script>/gi, ");
            if (data.length > 0)
            {
               if (ContentLocationInDOM && ContentLocationInDOM != null && ContentLocationInDOM != "null")
               {
                  $(‘#queryResultContainer’).html($(ContentLocationInDOM, data));
               }
               else
               {
                  $(‘#queryResultContainer’).html(data);
               }
            }
         }
      });
   }
</script>
</head>
<body>
<div id="queryResultContainer"/>

但我不想使用任何其他网站API来完成我的查询。可以看出,API用于获取其他网站的html。

我正在寻找的只是从网站中提取HTML body内容并在网页上将其解开的简单方法,但请求和响应都是客户端。应该没有服务器端脚本的干扰。请帮我解决你的建议。

0 个答案:

没有答案