我正在尝试抓取poorly designed governmental website,它使用从JavaScript触发的POST请求进行导航(我正在导航日历)。
我正在尝试以优雅的方式执行此操作,在节点中使用jsdom和jQuery(可能还有jsdom-simulant),但是我不确定我该如何在模拟器中触发事件,假设事件本身应该返回jsdom并触发新的HTTP POST请求。
我不希望你们为我编写代码,我只需要有关结构,原理或具有类似功能的现有代码库的几个指针即可。
答案 0 :(得分:1)
关于抓取部分,这是一个POST请求,用于发送表单url编码的数据。有效负载中似乎有2个字段是必需的:
__EVENTARGUMENT
的值每天都在增加。例如,在04/04/2018上是6668,在05/04/2018上是6669。查看最早的日期是1998年1月1日,索引是-730,因此可以使用差值来计算该索引从目标日期到1998年1月1日之间的天数,减去730
target_date="2018-04-04"
index=$(($(dateutils.ddiff 1998-01-01 "$target_date") - 730))
curl 'https://www.senat.ro/Voturiplen.aspx' \
-H 'User-Agent: Mozilla' \
-H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
--data "__EVENTTARGET=ctl00%24B_Center%24VoturiPlen1%24calVOT&__EVENTARGUMENT=$index"
并使用pup html解析器:
curl 'https://www.senat.ro/Voturiplen.aspx' \
-H 'User-Agent: Mozilla' \
-H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
--data "__EVENTTARGET=ctl00%24B_Center%24VoturiPlen1%24calVOT&__EVENTARGUMENT=$index" | \
pup 'table#ctl00_B_Center_VoturiPlen1_GridVoturi'
使用nodejs可以使用node-request,moment:
const request = require('request');
const moment = require('moment');
const jsdom = require("jsdom");
const {JSDOM} = jsdom;
var a = moment('21/12/2017', 'DD/MM/YYYY');
var b = moment('01/01/1998', 'DD/MM/YYYY');
var index = a.diff(b, 'days') - 730;
request.post({
url: 'https://www.senat.ro/Voturiplen.aspx',
form: {
"__EVENTTARGET": "ctl00$B_Center$VoturiPlen1$calVOT",
"__EVENTARGUMENT": index
},
headers: {
'User-Agent': 'Mozilla'
}
},
function(err, httpResponse, body) {
const dom = new JSDOM(body);
var table = dom.window.document.querySelector("#ctl00_B_Center_VoturiPlen1_GridVoturi");
console.log(table.textContent);
});