微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JSSoup 是否支持 select() 类似于 Beautiful Soup 或 JSoup?

如何解决JSSoup 是否支持 select() 类似于 Beautiful Soup 或 JSoup?

JSSoup(它本身声明“JavaScript + BeautifulSoup = jssoup”)是否支持类似于 Beautiful SoupJSoupselect() 操作以基于 CSS 选择器选择元素?

我没有找到它,它可能以不同的名称存在吗?

解决方法

从文档来看,它似乎被称为 findfindAll,具体取决于您要查找一个还是多个。这是他们给出的一个例子:

var data = `
<div>
  <p> hello </p>
  <p> world </p>
</div>
`
var soup = new JSSoup(data);
soup.find('p')
// <p> hello </p>

Looking at the source,我没有看到任何提供 CSS 选择器功能的东西,但它确实表明 findfindAll 接受多个参数,以及 {{3} 中的一个例子显示使用第二个参数按类过滤,例如:

const JSSoup = require('jssoup').default;
const data = `
<div>
    <p class="foo bar"> hello </p>
    <p> world </p>
</div>
`
const soup = new JSSoup(data);
console.log(soup.find('p','foo').toString()); // Logs: <p class="foo bar">hello</p>

第二个参数也可以用于其他属性,但 CSS 选择器似乎不是一个选项。

您还有其他选项,例如 the documentation for BeautifulSoup,其中包含所有常见的 DOM 内容,例如 querySelectorquerySelectorAll

const { JSDOM } = require("jsdom");
const data = `
<div>
    <p class="foo bar"> hello </p>
    <p> world </p>
</div>
`;
const dom = new JSDOM(data);
const doc = dom.window.document;
console.log(doc.querySelector(".foo").outerHTML); // Logs: <p class="foo bar"> hello </p>
,

您将无法使用类似于 querySelectorquerySelectorAll 的选择器查询。

这是 JSsoup 中的 findAll 定义:

{
  key: 'findAll',value: function findAll() {
    var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
    var attrs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
    var string = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
    // ...
    var strainer = new SoupStrainer(name,attrs,string);
    // ...
  }
}

这里是 SoupStrainer 构造函数:

function SoupStrainer(name,string) {
  _classCallCheck(this,SoupStrainer);

  if (typeof attrs == 'string') {
    attrs = { class: [attrs] };
  } else if (Array.isArray(attrs)) {
    attrs = { class: attrs };
  } else if (attrs && attrs.class && typeof attrs.class == 'string') {
    attrs.class = [attrs.class];
  }
  if (attrs && attrs.class) {
    for (var i = 0; i < attrs.class.length; ++i) {
      attrs.class[i] = attrs.class[i].trim();
    }
  }
  this.name = name;
  this.attrs = attrs;
  this.string = string;
  }

您需要将标签名称作为第一个参数传递,然后是属性。字符串被视为类名。

示例用法

const JSSoup = require('jssoup').default;

const html = `
<html>
  <head>
    <title>Hello World</title>
  </head>
  <body>
    <h1>Hello World</h1>
    <p class="foo">First</p>
    <p class="foo bar">Second</p>
    <div class="foo">Third</div>
  </body>
</html>
`;

const printTags = (tags) => console.log(tags.map(t => t.toString()).join(' '));

const soup = new JSSoup(html);

printTags(soup.findAll('p','foo'));
// <p class="foo">First</p> <p class="foo">Second</p>

printTags(soup.findAll('p',{ class: 'foo' }));
// <p class="foo">First</p> <p class="foo">Second</p>

printTags(soup.findAll('p',{ class: 'foo' },'Second'));
// <p class="foo">Second</p>

printTags(soup.findAll('p',{ class: ['foo','bar'] }));
// <p class="foo">Second</p>

printTags(soup.findAll(null,'bar'));
// <p class="foo bar">Second</p> <div class="foo">Third</div>
,

基于已经给出的答案,我只想补充一点:通过在undefined和{{中将标签名称设置为find(),还可以按类名称(没有标签名称)进行搜索1}}:

findAll()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?