首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过填网填网?

如何通过填网填网?
EN

Stack Overflow用户
提问于 2019-04-22 12:20:59
回答 1查看 61关注 0票数 0

如何通过网站登录表单,查看网站上任何相关网页的HTML代码。

我试图通过网站上的登录表单,然后解析html页面,在那里保存我的帐户信息,但我不能这样做。这是我的密码。

代码语言:javascript
复制
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代码。我想再写一页。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-22 12:46:57

问题是,cheerio无法跟踪新的url。

在您的具体案例中,有两种可能的解决方案:

  1. 使用浏览器登录网站,通过开发工具访问cookie并将其复制到request。像这样的东西来自文档
  2. 使用自动无头浏览器,它可以跟随页面重定向。并保存你的会话数据。比如木偶师或者

如果您已经使用了node.js,那么用傀儡机实现逻辑就更容易了。

这里是关于木偶师的更多信息。

更新

木偶技师:

代码语言:javascript
复制
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中从正式文件执行

代码语言:javascript
复制
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')
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55794339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档