美文网首页大数据 爬虫Python AI SqlPython小哥哥
基于Python打造账号共享浏览器!

基于Python打造账号共享浏览器!

作者: 14e61d025165 | 来源:发表于2019-05-31 15:09 被阅读0次

    本篇文章介绍的内容会涉及到以下知识:

    • PyQt5的使用;
    • Selenium的使用;
    • 代理服务器的架设和使用;

    文章目录

    一、账号限制之痛

    Python学习交流群:1004391443,这里有资源共享,技术解答,还有小编从最基础的Python资料到项目实战的学习资料都有整理,希望能帮助你更了解python,学习python

    在如今的互联网中,免费的信息和资源占据了很大一部分,各类互联网应用提供了各行各业的资讯和资源。这是互联网能够不断繁荣和扩大的重要原因之一。

    与此同时,一些收费或不公开的互联网应用则构成了互联网世界中更有价值和意义的部分。

    一些限制性较低的网站,可能仅仅需要进行用户登录即可使用服务;

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1559286565024" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    一些限制性中等的网站,则可能会出于账户安全或是其他方面的因素考虑,限制账号在一定时间一定IP范围内在进行登录使用;

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1559286565032" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    而一些限制性很高的网站,则可能需要使用到硬件设备(比如U盾、加密狗等)+账号进行登录,部分还会配合使用到浏览器插件。

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1559286565036" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    这些对账号登录的限制措施很大程度上确保了账户的安全性,但是却给使用者带来了很大的不便。

    出于某些需求,我们可能需要将一个包含限制登录和使用的账号分享给多个人同时使用,这时候,我们就可以借助Python来完美实现这个需求了。

    二、突破账号使用限制

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1559286565044" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    在这个方案中,我们需要使用到以下工具:

    • Selenium:用于模拟登录目标网站;
    • Cookie存储服务:用于存储和返回目标网站登录成功的Cookie;
    • 一个基于PyQt5或PiSide2的QWebEngine模块制作的浏览器;

    我们首先借助Selenium对目标网站进行模拟登录并不断维持登录状态,将登录成功和更新的Cookie上传给Cookie存储服务;

    然后使用PyQt5或PySide2借助其QWebEngine模块的浏览器核心自制一个浏览器。

    在启动的时候从Cookie存储服务中获取最新的Cookie,将获取到的Cookie配置为浏览器默认全局Cookie。

    这样,在我们打开目标网站的时候,默认就是已经登录后的状态了。如此就免除了登录限制;

    三、处理账号登录IP异常

    在上面提到的方案中,基本可以实现账号的共享,但是问题也是有的。比如:

    多个用户同时访问的时候,同一个Cookie会匹配给多个IP地址,这对于网站后台而言,可能会将其识别为多个用户登录,从而导致频繁掉线。

    多个用户访问,会使同一个账号出现多个IP地址,这对于一些安全等级较高的网站来说,会将其判定为存在疑似用户被盗号的风险,进行限制账号登录和访问。

    面对这种情况,我们则需要下面这种方案:

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1559286565049" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    在这个方案中,我们加入了一个关键的代理服务器。

    这个代理服务器会对包括Selenium和浏览器的所有请求进行转发。

    Selenium通过这个代理服务器模拟登录目标网站,

    自制的Qt浏览器也默认通过代理服务器访问目标网站。

    在加入代理服务器之后,无论是多少个用户同时访问目标网站,所呈现出来的IP地址永远只有代理服务器的IP地址这一个。

    这样,就避免了因为多个用户访问导致同一账号被多个IP地址使用,从而导致账号掉线和限制登录的风险了。

    大家还有什么好的方案,欢迎留言交流:)

    相关文章

      网友评论

        本文标题:基于Python打造账号共享浏览器!

        本文链接:https://www.haomeiwen.com/subject/jitgtctx.html