我正在使用 PhantomJS 从 Twitter 页面中提取一些数据。以下是我尝试抓取的示例内容:
<span class="ProfileTweet-action--reply u-hiddenVisually">
<span class="ProfileTweet-actionCount" data-tweet-stat-count="541">
<span class="ProfileTweet-actionCountForAria" data-aria-label-part>541 replies .</span>
</span>
</span>
这是我获取回复计数的代码:
var replyCount = page.evaluate(function(){
return document.getElementsByClassName("ProfileTweet-action--reply");
});
for (var i = 0; i < replyCount.length; i++) {
var replyInt = replyCount[i].innerText;
console.log(replyInt);
}
输出为541条回复
有没有办法只抓取data-tweet-stat-count
的值,这样我就可以获得“541”?
该页面中还有其他同名的元素data-tweet-stat-count
。有人可以指导我吗?
最佳答案
var replyCount = page.evaluate(function(){
return document.querySelector('span.ProfileTweet-action--reply span.ProfileTweet-actionCount').getAttribute('data-tweet-stat-count');
});
关于javascript - 通过特定元素名称抓取数据 - PhantomJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43409464/