1

使用此语法:

x('http://www.viadeo.com/fr/company/unicef', 
    '.page-content',
    [{
    img:'img@src',
    bio:'.pan-desc-description',

    org:'.pan-desc-footer-element @element-value',
    link: '.element-value a@href',
    **twitter:'.element-value a@href'** // I get the previous link not the twitter one 

}]).write('result.json')

网站中有多个具有该特定类名的项目,但它只返回第一个。有没有办法抓住所有这些,也许我可以用那个回报做一个 .limit ?如果它在文档中,我深表歉意,我已经通读了两次,看起来它在任何地方都没有明确说明。

4

2 回答 2

1

您只需要用括号将您想要“拆分”的内容包裹起来。

这是对我有用的代码。

var Xray = require('x-ray');
var x = Xray();

x('http://www.viadeo.com/fr/company/unicef',
    '.page-content',
    {
    img:'img@src',
    bio:'.pan-desc-description',

    org:'.pan-desc-footer-element @element-value',
    link: ['.element-value a@href'],
    //twitter:'.element-value a@href'

})
(function(err, title) {
  console.log(title)
});

我还删除了外部括号,因为页面内容永远不会有超过一个元素,所以你永远不会想要超过一个。

于 2017-08-12T05:09:56.037 回答
1

您可以利用 chrome 检查器工具来获得正确的选择器,

在这里,这段代码对我有用,

var Xray = require('x-ray');
var x = Xray();
x('http://www.viadeo.com/fr/company/unicef', 
'.page-content',
 [{
  img:'img@src',
  bio:'.pan-desc-description',
  org:'.pan-desc-footer-element @element-value',
  link: '.element-value a@href',
  twitter:'.mbs:nth-child(4) a@href' // or use div.element-value.gu.gu-last a@href
}]).write('result.json')

在那里,我们得到了这个结果。

[
  {
    "img": "http://static8.viadeo-static.com/fzv6VNzGukb7mt5oV0Nl-wQxCDI=/fit-in/200x200/filters:fill(white)/7766b960b98f4e85affdab7ffa9863c7/1434471183.jpeg",
    "bio": "Le Fonds des Nations unies pour l'enfance (abrégé en UNICEF ou Unicef pour United Nations International Children's Emergency Fund en anglais) est une agence de l'ONU consacrée à l'amélioration et à la promotion de la condition des enfants. Son nom était originellement United Nations International Children's Emergency Fund, dont elle a conservé l'acronyme. Elle a activement participé à la rédaction, la conception et la promotion de la convention relative aux droits de l'enfant (CIDE), adoptée suite au sommet de New York en 1989. Son revenu total en 2006 a été de 2 781 millions Dollar US.\r\n          L'UNICEF a reçu le prix Nobel de la paix en 1965.",
    "link": "http://www.unicef.org/",
    "twitter": "http://www.twitter.com/UNICEF "
  }
]

以下是如何在 chrome 上获得正确的选择器:

首先,您右键单击并单击检查。 在此处输入图像描述

然后单击复制选择器并使用它。 在此处输入图像描述

当你复制选择器时,它会说,

#pan-desc > div.pan-desc-grey > div > div:nth-child(4) > div.element-value.gu.gu-last > a

您可以直接使用它,也可以对其进行改进。

于 2017-08-12T05:25:12.550 回答