如何从维基百科表中获得第一列值的列表
How to get the list of the first column value from wikipedia table?
我试图在这里(开始)的第一个维基百科表的第一列的年份列表,并将其放置在一个选择
我正在以这种方式读取json,但我无法抓住我需要的东西,以便将其放置在select中:
$(document).ready(function(){
$.ajax({
type: "GET",
url: "https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text§ion=1&page=List_of_wars_1000%E2%80%931499&callback=?",
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
success: function (data, textStatus, jqXHR) {
var markup = data.parse.text["td"];
var i = $('<div></div>').html(markup);
// remove links as they will not work
i.find('a').each(function() { $(this).replaceWith($(this).html()); });
// remove any references
i.find('sup').remove();
// remove cite error
i.find('.mw-ext-cite-error').remove();
$('#article').html($(i).find('p'));
},
error: function (errorMessage) {
}
});
});
这里有一个解决方案:
$(document).ready(function(){
$.ajax({
type: "GET",
url: "https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text§ion=1&page=List_of_wars_1000%E2%80%931499&callback=?",
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
success: function (data, textStatus, jqXHR) {
var html = data.parse.text['*'];
if(!html) {
return;
}
var $hiddenContent = $('<div/>').html(data.parse.text['*']).hide();
var $firstColumnCells = $hiddenContent.find('table.wikitable').find('td:first-child');
$hiddenContent.remove(); // remove our helper div
var values = [];
$firstColumnCells.each(function(idx, cell) {
var val = $(cell).text().match(/'d+/)[0];
values.push($(cell).text());
// you can also do something here with the value
$('#article').append('<div>'+ val + '</div>');
});
// show as array in your console if you like or doSomething with the array
//console.log(values);
},
error: function (errorMessage) {
}
});
});
#article div {
padding: 5px;
margin: 5px 0;
background: grey;
width: auto;
color: white;
width: 100px;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="article">
<h2>Years</h2>
</div>
url的结果是一个名称为"*"而不是"td"的对象,所以您的行:
data.parse.text["td"]
是
data.parse.text["*"]
为您提供了本文的所有标记,您已经将其解析为html。您可以使用其他url,但这是提供的结果。
您可以使用jquery从文章中找到您想要的内容,例如:
html.find("table td:first-child")
从表中获取所有的第一列(您可能需要table:first
用于其他文章等)。
工作片段:
$(document).ready(function(){
$.ajax({
type: "GET",
url: "https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text§ion=1&page=List_of_wars_1000%E2%80%931499&callback=?",
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
success: function (data, textStatus, jqXHR) {
var markup = data.parse.text["*"];
var html = $('<div/>').html(markup);
var cells = html.find("table td:first-child");
cells.each(function() {
console.log($(this).text());
});
var years = cells.map(function() { return $(this).text(); }).get();
console.log(years.join(","))
},
error: function (errorMessage) {
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
您似乎在响应对象中选择了错误的属性td
试
var markup = data.parse.text['*'];
var i = $('<div></div>').html(markup);
var years = i.find('table:first tr:gt(1)').map(function() {
return $(this).children().eq(0).text()
}).get()
演示
您可以很容易地通过选择所有tr
中的td:first-child
来做到这一点。如果它是另一个字段,您可以使用td:nth-child(5)
伪选择器。
这里有一个纯javascript的例子;
var nodes = document.querySelectorAll(".wikitable tr td:first-child")
var values = Array.prototype.map.call(nodes, function(n){
return n.innerContent;
})
与jQuery中的类似,您可以这样做(未经测试);
var values = $(".wikitable tr td:first-child").each(function(n){
return n.innerContent;
})
你以后可以使用jQuery的wrap
函数使每个文本/年份成为一个选项元素,你可以传递给一个选择下拉菜单
相关文章:
- 柱状图后面的一列
- 数据表通过分析一列的值对其进行排序
- Datatables-如何在未显示所有列的情况下禁用表中最后一列的排序
- 获取表中每行的一列的内容,然后循环访问每个内容
- 对象数组-与其他列数据相比,增加一列
- 在最初的查询/表格绘制中只提取谷歌电子表格的一列
- jQuery Datatables:单击一列时对另一列进行排序
- 我的砖石图像只显示在一列中
- 删除“;使用javascript从pentaho中的一列中选择“[”
- 使用 Javascript 在一列中更改属性内容
- jquery插件 - 已选择 - 多项选择不在一行中列出,而是列在一列中
- 单击包含按钮的最后一列时获取表的第一列值
- 排除行选择中的最后一列
- 取消表中一列的onclick事件
- “搜索”文本仅考虑表格中的最后一列
- jqGrid有条件地创建一列
- 当第一次砌筑'项目'将所有剩余项隐藏在一列中
- 避免在rich:dataTable的某一列中出现onRowClick事件
- Kendo Grid UI水印列或一列的两个下拉列表
- 如果一列中有使用angular js的值列表,我如何在其中生成嵌套表