根据豆瓣某个名单随手一写,只写了从word文档中uid清单读取这个模块

代码

  1. # -*- coding:utf-8 -*-
  2. # Created by ZhaoWen on 2021/3/28
  3. import logging.config
  4. import requests
  5. from requests.cookies import RequestsCookieJar
  6. import json
  7. import time
  8. import docx
  9. import re
  10. def read_docx(path):
  11. '''
  12. 读取word文件
  13. :param path: file path
  14. :return: a list about user id
  15. '''
  16. file = docx.Document(path)
  17. user_list = []
  18. for para in file.paragraphs:
  19. user_path = para.text
  20. if re.match("https://www.douban.com/people/(.*)",user_path.strip()):
  21. user_list.append(user_path.split("/")[4])
  22. return user_list
  23. logging.config.fileConfig('logging.conf')
  24. logger = logging.getLogger()
  25. def follow_user(user_uid):
  26. '''
  27. 关注用户
  28. :param user_uid:
  29. :return:
  30. '''
  31. # 填入自己的cookie
  32. cookie = ""
  33. base_url = "https://www.douban.com/j/contact/addcontact"
  34. cookie_jar = RequestsCookieJar()
  35. ck = ""
  36. for c in cookie.split(";"):
  37. c_list = c.split("=")
  38. cookie_jar.set(c_list[0].strip(), c_list[1].strip())
  39. if c_list[0].strip() == "ck":
  40. ck = c_list[1]
  41. header = {
  42. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0',
  43. }
  44. post_data = {
  45. 'people': "",
  46. 'ck': ck
  47. }
  48. for uid in user_uid:
  49. post_data["people"] = uid
  50. response = requests.post(url=base_url, data=post_data, headers=header,cookies=cookie_jar)
  51. resp_msg = json.loads(response.text)
  52. if resp_msg["result"] == True:
  53. logging.info("https://www.douban.com/people/"+uid+"/....关注成功")
  54. else:
  55. logging.info("https://www.douban.com/people/"+uid+"/....关注失败或用户已关注")
  56. time.sleep(2)
  57. logging.info("操作成功~")
  58. if __name__ == "__main__":
  59. # 传入待文件地址 目前仅支持读取word文件
  60. path = input("请输入文件地址:")
  61. uid_list = read_docx(path)
  62. follow_user(uid_list)