通过Node.js服务器加载页面时如何更改html

时间:2018-08-07 11:14:36

标签: javascript node.js parsing server html-parsing

我需要拦截浏览器中的页面加载,以对具有特定属性的div进行调整。那些。我了解算法:访问页面时,处理request,稀疏整个HTML,找到具有div属性的data-attr,添加另一个data-newattr属性到它,然后将所有HTML代码发送到浏览器。在实践中,我不明白,tk。我仅在第二天使用Node.js。我们现在要做的是:创建一个处理请求并将响应发送到浏览器的服务器。但是,我发现的所有课程和示例都仅涉及发送标头(该主题当然很重要,但现在不是我所需要的)以及记录页面类型的任何数据。例如:

var http = require('http');
var url = require('url');
var server = new http.Server();
server.listen(80, '127.0.0.1');
server.on('request', function(req, res){
    var parsedUrl = url.parse(req.url, true);
    console.log(req);
    res.end(parsedUrl.query.q);
});

我还发现了如何使用HTMLrequest模块来解析cheerio,这使得可以使用所需页面的HTML进行操作,但是只能使调整并进一步使用(网页抓取)。

var request = require('request'), cheerio = require('cheerio');
request({uri:'https://amazon.com/', method:'GET', encoding:'binary'},
    function (err, res, body) {
        if (err) throw err;
        console.log(body);
        console.log(res.statusCode);
        var $ = cheerio.load(page);
        var img_src = $('div.s9a3 > div > div > a > div > div > img').attr("src");
        console.log(img_src);
});

如何将这两个功能结合在一起?要从第三方HTML那里获取URL,而不是从正在形成服务器端的页面获取HTML?以及如何将已编辑的res.end()发送到已经位于@Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/addPost").hasAnyRole("EMPLOYEE","ADMIN") .antMatchers("/profile").hasAnyRole("EMPLOYEE","ADMIN") .antMatchers("/leaders/**").hasRole("MANAGER") .antMatchers("/systems/**").hasRole("ADMIN") .antMatchers("/**").permitAll() .and().formLogin().loginPage("/login").defaultSuccessUrl("/") .loginProcessingUrl("/processLogin").permitAll().and().logout().permitAll() .and().exceptionHandling().accessDeniedPage("/access-denied"); } @Override public void configure(WebSecurity security){ security.ignoring().antMatchers("/css/**","/node_modules/**","/source/**","/style/**"); } 的浏览器中?

0 个答案:

没有答案