我有一个网站正在尝试自动从中抓取数据。我有一个合法帐户,需要通过编程收集数据来简化生活。问题是,对于伪造者代码的每次迭代,我的帐户都重新登录,并且经过几轮编码后,我从服务(MLS)收到一封电子邮件,他们注意到我的帐户存在可疑活动。 我的问题是,是否可以实时编写人偶代码脚本,以便我可以在同一会话中测试不同的代码行?
到目前为止,我已经尝试过:
const puppeteer = require('puppeteer');
const fs = require("fs");
const browser = puppeteer.launch({headless: false, ignoreHTTPSErrors: true, args: ['--start-maximized'], defaultViewport: null, slowMo: 150})
const page = browser.newPage();
page.goto('https://idp.sdmls.com/idp/Authn/UserPassword',{waitUntil: 'networkidle2'});
Chromium窗口打开,但没有其他显示。 我是在命令行窗口中看到的:
> const puppeteer = require('puppeteer');
undefined
> const fs = require("fs");
undefined
>
> const browser = puppeteer.launch({headless: false, ignoreHTTPSErrors: true, args: ['--start-maximized'], defaultViewport: null, slowMo: 150})
undefined
> const page = browser.newPage();
TypeError: browser.newPage is not a function
> page.goto('https://idp.sdmls.com/idp/Authn/UserPassword',{waitUntil: 'networkidle2'});
ReferenceError: page is not defined
>
答案 0 :(得分:0)
{
"response":{
"firstname":"Fir",
"frlogs":[
{
"action":"test3",
"dateverified":"2019-07-04 10:55",
"membershipappid":"0000000000",
"isverified":"true",
"lastverified":"2019-07-04 10:55",
"transactiondate":"2019-07-04"
}
]
}
}
public void sendPost() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("my url");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setRequestProperty("Accept","application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
JSONObject obj = new JSONObject();
JSONArray array = new JSONArray();
obj.put("action", "Success");
obj.put("dateverified", currentDateandTime);
obj.put("membershipappid", AppData.usrID);
obj.put("isverified", "True");
obj.put("lastverified", currentDateandTime);
obj.put("transactiondate", currentDateandTime);
array.put(obj);
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
os.writeBytes(obj.toString());
os.flush();
os.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
是一个异步函数,它返回puppeteer.launch
。这意味着您应该在Promise
函数中为此await
,或将后续代码放入async
。
由于要使用Node.js命令行,因此无法直接.then()
使用它。有几种选择:
选项1:将代码放入await
:
.then()
选项2(适合调试,但 不要尝试在实际代码中使用 ):将promise结果保存到全局对象中并重复使用。
$ node
Welcome to Node.js v12.1.0.
Type ".help" for more information.
> const options = {headless: false, ignoreHTTPSErrors: true, args: ['--start-maximized'], defaultViewport: null, slowMo: 150};
...
> const puppeteer = require('puppeteer');
undefined
> const browserPromise = puppeteer.launch(options);
undefined
> const pagePromise = browserPromise.then(browser => { return browser.newPage(); });
undefined
> pagePromise.then(page => { page.goto('http://stackoverflow.com'); });
Promise { <pending> }
等待一段时间,直到承诺解决并分配了全局对象,然后继续:
> puppeteer.launch(options).then(browser => { global.browser = browser; });
Promise { <pending> }
再次等待,然后继续:
> global.browser.newPage().then(page => { global.page = page; });
Promise { <pending> }