相关疑难解决方法(0)

除了XHTML自包含标记之外,RegEx匹配开放标记

我需要匹配所有这些开始标记:

<p>
<a href="foo">
Run Code Online (Sandbox Code Playgroud)

但不是这些:

<br />
<hr class="foo" />
Run Code Online (Sandbox Code Playgroud)

我想出了这个,并希望确保我做对了.我只抓住了a-z.

<([a-z]+) *[^/]*?>
Run Code Online (Sandbox Code Playgroud)

我相信它说:

  • 找一个小于,然后
  • 然后,查找(并捕获)az一次或多次
  • 然后找到零个或多个空格
  • 找到任何字符零次或多次,贪婪/,然后
  • 找到一个大于

我有这个权利吗?更重要的是,你怎么看?

html regex xhtml

1323
推荐指数
36
解决办法
270万
查看次数

如何在不使用XmlService的情况下解析Google Apps脚本中的HTML字符串?

我想使用Google Spreadsheets和Google Apps脚本创建一个刮刀.我知道这是可能的,我已经看过一些关于它的教程和线程.

主要想法是使用:

  var html = UrlFetchApp.fetch('http://en.wikipedia.org/wiki/Document_Object_Model').getContentText();
  var doc = XmlService.parse(html);
Run Code Online (Sandbox Code Playgroud)

然后开始使用这些元素.但是,方法

XmlService.parse()
Run Code Online (Sandbox Code Playgroud)

对某些页面不起作用.例如,如果我尝试:

function test(){
    var html = UrlFetchApp.fetch("https://www.nespresso.com/br/pt/product/maquina-de-cafe-espresso-pixie-clips-preto-lima-neon-c60-220v").getContentText();
    var parse = XmlService.parse(html);
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Error on line 225: The entity name must immediately follow the '&' in the entity reference. (line 3, file "")
Run Code Online (Sandbox Code Playgroud)

我试图用来string.replace()消除显然导致错误的字符,但它不起作用.出现所有其他错误.以下代码为例:

function test(){
    var html = UrlFetchApp.fetch("https://www.nespresso.com/br/pt/product/maquina-de-cafe-espresso-pixie-clips-preto-lima-neon-c60-220v").getContentText();
    var regExp = new RegExp("&", "gi");
    html = html.replace(regExp,"");

    var parse = XmlService.parse(html);
}
Run Code Online (Sandbox Code Playgroud)

给我以下错误:

Error on line 358: The content of elements must …
Run Code Online (Sandbox Code Playgroud)

javascript parsing html-parsing google-sheets google-apps-script

15
推荐指数
3
解决办法
2万
查看次数

如何使用 CSS 选择器解析 HTML 字符串?

我正在尝试从 HTML 页面中提取字符串变量中的一些数据。我知道Google Apps脚本代码是在服务器端执行的,并且document对象没有定义。

是否有其他简单的方法可以使用 CSS 选择器(如 ) 来提取数据document.querySelector

css-selectors html-parsing google-apps-script

5
推荐指数
0
解决办法
2454
查看次数