[英]Web scraping an HTML table in Cheerio
我的網頁抓取項目有問題。 這是我需要抓取的頁面示例:
<table style="position...">
<thead>..</thead>
<tbody id="leaderboard_body">
<tr bgcolor="#155555">..</tr>
<tr bgcolor="#155555">..</tr>
<tr bgcolor="#155555">..</tr>
...
</tbody>
</table>
我想訪問tr標記中的信息,但無法實現。 我找不到像這樣的簡單代碼的tbody標簽,而且我也不知道為什么:
var cheerio = require("cheerio"); var url = "http://www.dota2.com/leaderboards/?l=french#europe"; var http = require("http"); // Utility function that downloads a URL and invokes // callback with the data. function download(url, callback) { http.get(url, function(res) { var data = ""; res.on('data', function (chunk) { data += chunk; }); res.on("end", function() { callback(data); }); }).on("error", function() { callback(null); }); } download(url, function(data) { if (data) { var $ = cheerio.load(data); var content = $('tbody').text(); console.log(content); } else console.log(err); });
這是因為該表在HTML中不存在,它是在頁面加載后用javascript插入的,並且無法以傳統方式進行抓取。
始終查看源代碼,而不僅僅是控制台中的實時視圖。
僅做最少的研究就表明該表是根據JSON構建的,該JSON是通過請求
http://www.dota2.com/webapi/ILeaderboard/GetDivisionLeaderboard/v0001?division=europe
這樣,您就可以對所有需要的數據進行預格式化和准備好了,而無需刮擦HTML
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.