javascript - 通过特定元素名称抓取数据 - PhantomJS

标签 javascript web-scraping phantomjs

我正在使用 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/

相关文章:

node.js - 如何在不同的 casperjs 进程之间持久化 cookie

javascript - 将 Google map 搜索结果(商店定位器)显示为无序列表,?

python - beautifulsoup - 删除一行代码

c++ - 网页加载时获取m3u8资源的url

javascript - 当在 phantomjs 中发出某些请求时,chrome、firefox、ie 浏览器中存在的 google cookie 是否会发送?

c# - Selenium.PhantomJS 是无效的命名空间

php - Android 和 JavaScript : How to know in javascript that an application is installed in the Android device or not?

javascript - 如何使用 ElasticSearch 索引源代码

javascript - 使用 Javascript 显示时背景图像显示为白色

xpath - scrapy xpath 按类名选择元素