浏览 365 次
锁定老帖子 主题:出行也需要注意疫情的防护
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2021-07-28
最近朋友圈不是被大家出去玩的足迹刷屏了吗,惊叹于那些把全国所有省基本走遍的朋友。刚好又看到新增案例也是因为出去玩导致的,所以就萌生了写一篇出行相关的内容,本次数据来源于一个对于爬虫十分友好的出行攻略类网站:蚂蜂窝 这里我们主要分享下数据的抓取过程,话不多说,上代码: const http = require("http"); const url = require("url"); // 要访问的目标页面 const targetUrl = "https://www.mafengwo.cn/"; const urlParsed = url.parse(targetUrl); // 代理服务器(产品官网 www.16yun.cn) const proxyHost = "t.16yun.cn"; const proxyPort = "36600"; // 生成一个随机 proxy tunnel var seed = 1; function random() { var x = Math.sin(seed++) * 10000; return x - Math.floor(x); } const tunnel = random()*100; // 代理验证信息 const proxyUser = "16MCUWZB"; const proxyPass = "149750"; const base64 = new Buffer.from(proxyUser + ":" + proxyPass).toString("base64"); const options = { host: proxyHost, port: proxyPort, path: targetUrl, method: "GET", headers: { "Host": urlParsed.hostname, "Proxy-Tunnel": tunnel, "Proxy-Authorization" : "Basic " + base64 } }; http.request(options, function (res) { console.log("got response: " + res.statusCode); res.pipe(process.stdout); }).on("error", function (err) { console.log(err); }).end(); 这里我们还是需要注意下,网站虽然友好,但不代表人家会让你肆无忌惮的访问呀,所以我们基本的反爬策略之爬虫代理必须要挂上的,不要问小编哪家的质量好,问就是代码里面提供的这家。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |