如何通过网站登录表单,查看网站上任何相关网页的HTML代码。
我试图通过网站上的登录表单,然后解析html页面,在那里保存我的帐户信息,但我不能这样做。这是我的密码。
const express = require('express');
const fs = require('fs'); //access to file system
const request = require('request');
const cheerio = require('cheerio');
const rp = require('request-promise');
const app = express();
let url = 'url';
(request.post({url:'url1', form: {
email:'email',
password:'password'
}},
function(error, response, html){
if(error){
console.log(error);
}
else{
console.log(html);
}
}))
app.get('/scrape', function(req, res){
requestToWork(url);
res.send('Check your console!')
})
function requestToWork(url){
return rp(url)
.then(HTMLresponse=>{
const $ = cheerio.load(HTMLresponse);
console.log($.text());
$('.ellipsis').each((i, element) => {
console.log(element);
});
})
}
app.listen('8080')
console.log('Listening port 8080');
exports = module.exports = app;它只是从登录页面登录到我的HTML代码。我想再写一页。
发布于 2019-04-22 12:46:57
问题是,cheerio无法跟踪新的url。
在您的具体案例中,有两种可能的解决方案:
如果您已经使用了node.js,那么用傀儡机实现逻辑就更容易了。
这里是关于木偶师的更多信息。
更新
木偶技师:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
// Now you have two ways
// First one with evaluate, to access page DOM
await page.evaluate(() => {
// Here you have access to DOM. So you can make any JS DOM operations, you wish.
const form = document.querySelector('form');
const email = document.querySelector('email');
// ...some actions
form.submit();
})
// The second one, with puppeteer helper functions
const email = await page.$('email');
// Type function will type text in input
await elementHandle.type('some text');
// press function will emulate enter button press.
await elementHandle.press('Enter');
await page.waitFor(1500);
// Here you have result of your auth procedure.
// After all your operations, just close the browser.
await browser.close();
})();这里是关于傀儡类型的
如果我们正在寻找request实现。
首先我们得去拿饼干。
您可以通过这铬扩展解压缩cookies,或者转到开发工具、Network选项卡,单击first record并在Request Headers部分查找Cookie标头。
只需复制它
然后,在代码中像这样从request中从正式文件执行
const j = request.jar();
// Here 'key1=value1' change with your cookie from browser
const cookie = request.cookie('key1=value1');
const url = 'http://www.google.com';
j.setCookie(cookie, url);
request({url: url, jar: j}, function () {
request('http://images.google.com')
})https://stackoverflow.com/questions/55794339
复制相似问题