javascript - 如何在 Cheerio 中执行索引以进行网页抓取
问题描述
我正在使用 Cheerio 进行网页抓取,我之前使用过 bs4。
我想抓取https://rera.kerala.gov.in/rera_project_details这个网站;在 Python 中刮表,我们可以使用 findall("tr")[0] 来获取 first <tr>
。
但是如何在 Cheerio 中执行相同的操作?
下面是我的代码:
var request = require('request');
var cheerio = require('cheerio');
var fs = require('fs');
const url = "https://rera.kerala.gov.in/rera_project_details";
const arr = [];
request({method:"GET",url}, function(err, res, body){
if (res.statusCode==200){
let $ = cheerio.load(body);
const getID = $("#block-zircon-content");
const tbody = getID.find('tbody');
tbody.each((i, el)=>{
const ff = $(el).find("tr");
console.log(ff.html());//it returns first tr
//how to get 2 tr so that i can get td of second tr and can inde on td also
})
}}
)
如果我遍历它返回所有 tr ,现在如何索引每个 td 以便在表的最后一列中我可以获得获取 pdf 的链接?
编辑
我已经到了这里,但是如何获取 tr 中的 td 元素列表:
const getID = $(".views-table");
const getBody = getID.find("tbody");
const gettr = getBody.find("tr");
const getfirsttr = $.html(gettr[0]);//it gives me first tr
const getfirsttd = getfirsttr.find("td")//does not work
解决方案
要回答索引问题:
$('tr').eq(n)
会给你第n个tr作为cheerio对象。和
$('tr')[n]
将其作为 parse5 对象
推荐阅读
- azure - 无法在我的 Azure 实例上创建表单识别器资源
- ios - 迅速。如何遍历字典值:var a:[String:[String]]?
- python - Pandas 返回在另一列中找到子字符串的行列表
- html - 为什么我不能在带有 href 属性的 html 标签中使用 file://?
- python - 将键发送到自动完成文本框
- google-ads-api - 将着陆页网址更改为新域会影响 Google Ads SEM 的效果吗?
- scala - SCALA - 如何在结构中调用函数?
- c# - 在 wpf 核心中将 XAML 属性绑定到我的 DataContext (ViewModel) 之外的自定义对象属性
- snowflake-cloud-data-platform - 您如何从不同的 AgeGroup 中选择多个随机行?
- javascript - 反应 redux 并结合 reducer