0

我无法通过 javascript 正则表达式替换清除一些 html。任务是从本地来源获取我的 XBMC 的电视列表。URL 是http://tv.dir.bg/tv_search.php?step=1&all=1保加利亚语)。我正在尝试使用刮板来获取数据 - http://code.google.com/p/epgss/(归功于 Ivan Markov - http://code.google.com/u/113542276020703315321/)不幸的是自上述工具上次更新以来,电视列表页面已更改,因此我正在尝试使其正常工作。问题是当我尝试从 HTML 解析 XML 时,它会中断。我现在试图通过正则表达式替换头部和脚本标签来清理 html。不幸的是,它不起作用。这是我的替代品:

function regexReplace(pattern, value, replacer) 
{  
var regEx = new RegExp(pattern, "g");  
var result = value.replaceAll(regEx, replacer);  
if(result == null)  
return null;  
return result;  
}  

这是我的电话:

var htmlStringCluttered = HTML.getHTML(new URL(url), "WINDOWS-1251");  
log("Content grabbed (schedule for next 7 days)");  
log(url);  
var htmlString = regexReplace("<head>([\\s\\S]*?)<\/head>|<script([\\s\\S]*?)<\/script>", htmlStringCluttered, "");  

getHTML 函数来自原始源代码,我对设置 User-Agent 进行了细微修改。这是它的基础:

    public static java.io.Reader open(URL url, String charset) throws UnsupportedEncodingException, IOException  
    {
    URLConnection con = url.openConnection();
    con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.38 Safari/532.0");
    con.setAllowUserInteraction(false);
    con.setReadTimeout(60*1000/*ms*/);

    con.connect();

    if(charset == null && con instanceof HttpURLConnection) {
        HttpURLConnection httpCon = (HttpURLConnection)con;
        charset = httpCon.getContentEncoding();
    }

    if(charset == null)
        charset = "UTF-8";

    return new InputStreamReader(con.getInputStream(), charset);
    }

regexReplace 的结果和原来的完全一样。由于无法解析 XML,因此脚本无法读取元素。有任何想法吗?

4

1 回答 1

1

更新:

要将其转换为 XMLDocument,您可以执行以下操作:

var parseXml,
    xml,
    htmlStringCluttered = HTML.getHTML(new URL(url), "WINDOWS-1251"),
    htmlString = '';

if (typeof window.DOMParser != "undefined") {
    parseXml = function (xmlStr) {
        return (new window.DOMParser()).parseFromString(xmlStr, "text/xml");
    };
} else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) {
    parseXml = function (xmlStr) {
        var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlStr);
        return xmlDoc;
    };
} else {
    throw new Error("No XML parser found");
}

console.log("Content grabbed (schedule for next 7 days)");
console.log(url);

//eliminate the '<head>' section
htmlString = htmlStringCluttered.replace(/(<head[\s\S]*<\/head>)/ig, '')

//eliminate any remaining '<script>' elements
htmlString = htmlString.replace(/(<script[\s\S]+?<\/script>)/ig, '');

//self-close '<img>' elements
htmlString = htmlString.replace(/<img([^>]*)>/g, '<img$1 />');

//self-close '<br>' elements
htmlString = htmlString.replace(/<br([^>]*)>/g, '<br$1 />');

//self-close '<input>' elements
htmlString = htmlString.replace(/<input([^>]*)>/g, '<input$1 />');

//replace '&nbsp;' entities with an actual non-breaking space
htmlString = htmlString.replace(/&nbsp;/g, String.fromCharCode(160));

//convert to XMLDocument
xml = parseXml(htmlString);

//log new XMLDocument as output
console.log(xml);

//log htmlString as output
console.log(htmlString);
  • 信用到期的信用:在以下位置parseXml找到的功能:

JavaScript中变量字符串的XML解析

您可以在浏览器中测试它(我做过:)),只需定义htmlStringCluttered为:

htmlStringCluttered = document.documentElement.innerHTML;

代替:

htmlStringCluttered = HTML.getHTML(new URL(url), "WINDOWS-1251"),

并在控制台中运行它http://tv.dir.bg/tv_search.php?step=1&all=1

您还必须注释掉该行:

console.log(url);

或声明url并赋予它一个值。

原来的:

您的 RegExp 需要做一些工作,当分成两个replace语句时,它会更简单(也更容易阅读):

var htmlStringCluttered = HTML.getHTML(new URL(url), "WINDOWS-1251"),
    htmlString = '';
console.log("Content grabbed (schedule for next 7 days)");
console.log(url);
//eliminate the '<head>' section
htmlString = htmlStringCluttered.replace(/(<head[\s\S]*<\/head>)/ig, '')
//eliminate any remaining '<script>' elements
htmlString = htmlString.replace(/(<script[\s\S]+?<\/script>)/ig, '');
//log remaining as output
console.log(htmlString);

通过访问http://tv.dir.bg/tv_search.php?step=1&all=1并在控制台中运行以下命令在控制台中对此进行了测试:

console.log(document.documentElement.innerHTML.replace(/(<head[\s\S]*<\/head>)/ig, '').replace(/(<script[\s\S]+?<\/script>)/ig, ''));

如果这是在outerHTML属性上运行的(正如我期望的HTML.getHTML(new URL(url), "WINDOWS-1251")方法返回),那么<body>元素将被包装在:

<html xmlns="http://www.w3.org/1999/xhtml">
    <body>
        ...
    </body>
</html>
于 2012-10-27T22:50:12.580 回答