<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Python實戰之自動化評論 玩csdn博客一個多月了,漸漸發現了一些有意思的事,經常會有人用同樣的評論到處刷,不知道是為了加沒什么用的積分,還是純粹為了表達樓主好人。那么問題來了,這種無聊的事情當然最好能夠自動化咯,自己也來試了一把,純屬娛樂。 ### 登陸 要評論當然要能夠先進行登陸,采用 [requests](http://docs.python-requests.org/en/latest/) 庫進行處理,嘗試能否看到自己的消息列表: ~~~ msg_url ="http://msg.csdn.net/" r = requests.get(msg_url, auth=('drfish', 'password')) ~~~ 結果跳轉到登陸界面,好的那看一下登陸界面是怎么登陸的,找到表單: ![csdn-login-form](https://box.kancloud.cn/2016-04-21_571879cd557af.jpg "") 發現還有一些隱藏的參數,如lt、excution等,好心的程序猿還寫明了不能為什么不能直接認證的原因:缺少流水號,那就多訪問一次來獲取流水號好了,用 [BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/bs4/doc/) 來分析頁面內容抓取流水號,同時因為要跨不同的域來進行操作,所以引入session: ~~~ msg_url = "http://msg.csdn.net/" login_url = "https://passport.csdn.net/" headers = { 'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} session = requests.session() session.headers.update(headers) r = session.get(login_url) page = BeautifulSoup(r.text, "lxml") authentication = { "username": "drfish", "password": "password", "lt": page.select("[name=lt]")[0]["value"], "execution": page.select("[name=execution]")[0]["value"], "_eventId": "submit", } r = session.post(login_url, authentication) r2 = session.get(msg_url) print(r2.text) ~~~ 好了,現在能夠得到我的消息信息了,說明已經成功解決登陸問題,那么自動化水軍評論應該就近在眼前了。 ### 自動評論 這次學乖了,隨便找了篇文章直接查看評論框form: ![csdn-comment-form](https://box.kancloud.cn/2016-04-21_571879cd786e5.jpg "") 在上面登陸代碼的基礎上進行評論的提交: ~~~ blog_url = "http://blog.csdn.net/u013291394/comment/submit?id=50444369" comment = { "comment_content": "水軍評論測試", "comment_usrId":"531203" } r2 = session.post(blog_url, comment) print(r2.text) ~~~ 結果返回了 `{"result":0,"content":"評論內容沒有填寫!","callback":null,"data":null}` 這樣的結果。有點意思,應該是在js中對參數進行了處理。那就把js拉出來看看,網頁里搜了一下js文件,有個 [comment.js](http://static.blog.csdn.net/scripts/comment.js) ,就是它了。在上面的form中可以看到提交時調用了subform方法,查看方法如下: ~~~ function subform(e) { if (c_doing) return false; var content = $.trim($(editorId).val()); if (content == "") { commentTip("評論內容沒有填寫!"); return false; } else if (content.length > 1000) { commentTip("評論內容太長了,不能超過1000個字符!"); return false; } var commentId = $("#commentId").val(); commentTip("正在發表評論..."); var beginTime = new Date(); $(editorId).attr("disabled", true); $("button[type=submit]", e).attr("disabled", true); c_doing = true; $.ajax({ type: "POST", url: $(e).attr("action"), data: { "commentid": commentId, "content": content, "replyId": $("#comment_replyId").val(), "boleattohome": $("#boleattohome").val() }, success: function (data) { c_doing = false; commentTip(data.content); if (data.result) { var rcommentid=$("#comment_replyId").val() $(editorId).val(''); $("#comment_replyId,#comment_verifycode").val(''); commentscount++; loadList(1, true); $(editorId).attr("disabled", false); $("button[type=submit]", e).attr("disabled", false); commentTip("發表成功!評論耗時:" + (new Date() - beginTime) + "毫秒") if (rcommentid!=undefined && rcommentid != "") { $("html,body").animate({ scrollTop: $("#comment_item_" + rcommentid).offset().top }, 1000); } } } }); return false; } ~~~ 可以清楚的看到最后POST提交的數據 `data` 改變了參數的名字,還有幾個其他的參數通過看js文件可以看到不是空的就是定死的,就不用管他了。同時發現上的 `"comment_usrId"` 也是給死的?那就只要comment一個變量就搞定了。 ~~~ blog_url = "http://blog.csdn.net/u013291394/comment/submit?id=50444369" comment = { "content": "水軍評論測試", } r2 = session.post(blog_url, comment) print(r2.text) ~~~ 看一下效果: ![![csdn-auto-comment][]](https://box.kancloud.cn/2016-04-21_571879cda9521.jpg "") ### 自動化 當然上面最終的參數傳遞也可以自己手動評論并用抓包軟件抓取,不過通過查看 `commetn.js` 文件也給我的自動化評論提供了方向,其中有一個 `load_comment_form()` 方法,是用來加載comment-form的,它給出了action的定義: `action="/' + username + '/comment/submit?id=' + fileName + '"` 寫的很明白了,我只要抓取到頁面的作者名和文章的編號就可以盡情的水評論了,隨便選個抓取文章的入口,如最新博客入口 [http://blog.csdn.net/?ref=toolbar_logo](http://blog.csdn.net/?ref=toolbar_logo) ,用BeautifulSoup抓取url并解析取到其中的username和filename來構成action并提價評論。 運行腳本試一下效果: ![csdn-comment-example](https://box.kancloud.cn/2016-04-21_571879cdbe3a9.jpg "") 打開評論管理看一下: ![example](https://box.kancloud.cn/2016-04-21_571879cdd4f90.jpg "") 自動化評論成功。 ### 寫在最后 寫這篇文章只是為了證明一下自己的想法,不是用來也**不希望有人用來惡意刷評論**。 - 本文由 DRFish([http://www.drfish.me/](http://www.drfish.me/))原創,轉載請寫明原鏈接,謝謝。 需要參考源碼請訪問我的[Github](https://github.com/gavinfish/Awesome-Python/tree/master/HotBlog) ([https://github.com/gavinfish/Awesome-Python/tree/master/HotBlog](https://github.com/gavinfish/Awesome-Python/tree/master/HotBlog))。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看