我在实现一个取消关注的功能,但是每次都是”系统繁忙“
chenguandan opened this issue · comments
chen guandan commented
代码很简单,就是在m.weibo_jump_from_com.py里添加一个函数。
def unfollow(self):
follow_url = 'https://weibo.com/841007111'
follow_params = {
# 'from':'myfollow_all',
'is_all':'1',
# 'noscale_head':'1'
}
follow_header ={
# "Host": "weibo.com",
# 'Upgrade-Insecure-Requests':'1',
"User-Agent": agent
}
r= self.session.get(follow_url, params=follow_params, headers=follow_header)
weibo_pa = r'<title>(.*?)</title>'
userID = re.findall(weibo_pa, r.content.decode("utf-8", 'ignore'), re.S)[0]
print('打开关注者页面', r.status_code, userID)
url = 'https://weibo.com/aj/f/unfollow'
unfollow_params = {
'ajwvr': '6'
}
unfollow_header = {
'Content-Type':'application/x-www-form-urlencoded',
'Referer': r.url,
"Host": "weibo.com",
"Origin":"https://weibo.com",
"User-Agent": agent,
"X-Requested-With":"XMLHttpRequest"
}
msg = '系统繁忙,请稍候再试吧。'
try_time = 0
while '系统繁忙' in msg and try_time<3:
try_time += 1
import time
time.sleep(3*try_time)
unfollow_r = self.session.post(url, params=unfollow_params, headers=unfollow_header)
content = unfollow_r.content.decode("utf-8", 'ignore')
import json
content = json.loads(content)
print('尝试取消关注', userID)
if 'msg' in content:
msg = content['msg']
print(msg)
xchaoinfo commented
@chenguandan 你 Post 的URL 地址不对,我这个登录最后是登录到 m.weibo.cn 的,你用 weibo.com 的URL ,这导致 Cookies 不匹配,当然有问题了。