vic*_*ory 0 html javascript css-selectors phantomjs
这是我用来从网页获取所有文本内容的代码。但它不起作用,我不知道我在做什么错。
<tr style="color:#000000" class="odd">
<td style="padding:5px 5px 5px 10px" align="center"><input type="checkbox" name="cards[]" id="card_278002" value="278002"></td>
<td align="center">411756</td>
<td align="center">Sherrie</td>
<td align="center">89852</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
那就是我的Js代码:
function get42() {
return document.querySelectorAll('tr>td').textContent;
}
console.log(page.evaluate(get42));
Run Code Online (Sandbox Code Playgroud)
输出:null..我在做什么错?
你不能那样使用document.querySelectorAll。返回NodeList。您必须textContent自己从每个Node中获取。
更长的路:
function get42() {
var tds = document.querySelectorAll('td'),
result = [];
for (var i = 0; i < tds.length; i++) {
result.push(tds[i].textContent);
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
或更短:
function get42() {
var tds = document.querySelectorAll('td');
return Array.prototype.map.call(tds, function(t) { return t.textContent; });
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4121 次 |
| 最近记录: |