Last active
June 6, 2016 20:29
-
-
Save FGFW/18a2be7464a042b5a568c61b8a3520a2 to your computer and use it in GitHub Desktop.
python3批量删除豆瓣分组下的好友
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| python3批量删除豆瓣分组下的好友 | |
| 2016年6月7日 03:43:42 codegay | |
| 我两年前一时冲动在豆瓣关注了很多豆瓣的员工,好多,有四百个。 | |
| 我现在一时冲动想取消关注...,写这么一个脚本可以用来加快删除的速度。 | |
| cookies还是直接从chrome读取出来, | |
| 参考我之前刚写的代码 python3从chrome浏览器读取cookie, | |
| """ | |
| import os | |
| import sqlite3 | |
| import re | |
| import requests | |
| from win32.win32crypt import CryptUnprotectData | |
| def getcookiefromchrome(host='.oschina.net'): | |
| cookiepath=os.environ['LOCALAPPDATA']+r"\Google\Chrome\User Data\Default\Cookies" | |
| sql="select host_key,name,encrypted_value from cookies where host_key='%s'" % host | |
| with sqlite3.connect(cookiepath) as conn: | |
| cu=conn.cursor() | |
| cookies={name:CryptUnprotectData(encrypted_value)[1].decode() for host_key,name,encrypted_value in cu.execute(sql).fetchall()} | |
| print(cookies) | |
| return cookies | |
| #运行环境windows 2012 server python3.4 x64 pywin32 chrome 50 | |
| #getcookiefromchrome() | |
| #getcookiefromchrome('.baidu.com') | |
| dbcookies=getcookiefromchrome('.douban.com') | |
| txt=requests.get('https://www.douban.com/contacts/list?tag=1718',cookies=dbcookies).text | |
| #print(txt) | |
| userid=re.findall(r'id="u(\d+)"',txt) | |
| ck=dbcookies['ck'] | |
| #ck的值在每次重新登录豆版后会变化。 | |
| #可以从网页中提取,不过我发现cookies也记录有了。直接提取出来就好了 | |
| head={'Content-Type':'application/x-www-form-urlencoded',} | |
| for uid in userid: | |
| data="people=%s&ck=%s" % (uid,ck) | |
| #data='people=47362624&ck=jeGZ' | |
| print(data) | |
| rs=requests.post('https://www.douban.com/j/contact/removecontact',headers=head,cookies=dbcookies,data=data).text | |
| print(rs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment