? ? ?很無聊惡作劇的想法,剛注冊github不久,空空蕩蕩的,沒有關(guān)注人,也沒有人關(guān)注。正好也算是練習(xí)python和scrapy了,萌生了想法,批量follow用戶,可以從一個(gè)明星用戶的follower頁面開始,批量follow。
? ? ? 兩個(gè)知識點(diǎn),一是登錄,二是關(guān)注。登錄見上篇文章,這里說下關(guān)注,依然是采用chrome開發(fā)者工具network欄,找post的數(shù)據(jù)和網(wǎng)址。網(wǎng)址形式為https://github.com/users,post的數(shù)據(jù)的payload為?

request headers 的Content-Type為multipart/form-data類型,但是scrapy的FormRequest的post類型為application/x-www-form-urlencoded,可見其實(shí)現(xiàn)

查了好多資料,斷續(xù)兩三天時(shí)間,學(xué)習(xí)了scrapy? post?multipart/form-data類型的方法,首先是要使用?requests_toolbelt庫中的 MultipartEncoder類,以實(shí)現(xiàn)post的payload。
構(gòu)建payload:
數(shù)據(jù)字段為utf8,anthenticity_token,取出網(wǎng)頁中的anthenticity_token字段,兩個(gè)字段構(gòu)成字典send_data

然后以send_data為參數(shù)構(gòu)建MultipartEncoder對象,這構(gòu)成了payload內(nèi)容。然后構(gòu)建headers,url,具體代碼如下

上述既是follow某個(gè)用戶的具體方法。批量關(guān)注github用戶,只要對follower頁面關(guān)注用戶批量關(guān)注就可以了。