关于抓取部分,这是一个 POST 请求发送表单 url 编码数据。有效载荷中有 2 个字段似乎是必需的:
- __EVENTTARGET=ctl00$B_Center$VoturiPlen1$calVOT
- __EVENTARGUMENT=XXXX(带有 XXXX 一些值)
__EVENTARGUMENT 值每天都在递增。例如,2018 年 4 月 4 日为 6668,2018 年 5 月 4 日为 6669。查看最早的日期,即 1998 年 1 月 1 日,索引为 -730,因此可以使用差值计算该索引目标日期和 1998 年 1 月 1 日之间的天数减去 730
使用 curl & bash 和 dateutils :
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);
});
检查this post 与moment 的日期差异