如何从维基百科页面中提取数据

问题描述 投票:-1回答:1

使用JavaScript从这个维基百科页面的表中提取数据的最佳方法是什么?

https://en.wikipedia.org/wiki/Most_common_words_in_Spanish

我已经尝试使用以下代码来获取JSON,但它没有奏效。然后,一旦我获得JSON,我将如何从表中获取数据?

fetch('https://en.wikipedia.org/wiki/Most_common_words_in_Spanish')
  .then(function(response) {
    return response.json();
  })
  .then(function(response){
  	console.log(response)
  })
javascript jquery web-scraping wikipedia
1个回答
0
投票

这段代码将把你的表作为html节点:

var url = 'https://en.wikipedia.org/w/api.php?action=parse&format=json&origin=*&page=Most%20common%20words%20in%20Spanish';

fetch(url)
  .then(function(response) {
    return response.json();
  })
  .then(function(response){
    html_code = response["parse"]["text"]["*"];
    parser = new DOMParser();
    html = parser.parseFromString(html_code, "text/html");
    var tables = html.querySelectorAll(".wikitable");
    console.log(tables);
  })

我正在使用MediaWiki API获取维基百科页面的html作为json响应。您可以找到此类API请求here的文档。

© www.soinside.com 2019 - 2024. All rights reserved.