0

私はCasperJS でリンクスクレーパーを構築しています。主な機能は次のようになります。

function findLinks() {
    return Array.prototype.map.call(document.querySelectorAll('a'), function(e){
        return { 
                 href: e.href,
                 title: e.title, 
                 rel: e.rel, 
                 anchor: e.text,
                 innerHTML: e.innerHTML
               };
    });
}

findLinks()ただし、リンクスクレーパーが次のようなものを見つけた場合は、次のように変更したいと思います。

<a href="#" title="anchor tag" rel="nofollow"><img src="myimage.jpg" alt="beautiful image" /></a>

<img>リンクの場合と同じように、属性に個別にアクセスできます。

私は Mozilla MDN と CasperJS を読んでいますが、これを達成する方法をまだ見つけていません。

どんな助けでも大歓迎です!

4

2 に答える 2

1

あなたが探していますElement.children

childrenは、指定された要素の子要素のコレクションを返します。

あなたの例のHTMLでは:

var b = document.querySelectorAll('a')[0];
alert(b.children[0].src); //First child's source: myimage.jpg

(フィドル)

于 2013-06-21T01:34:53.657 に答える