我尝试使用请求登录Twitter。我有一个工作代码用PHP登录Twitter,所以我试图转换它,但我看不出我做错了什么
import requests
s = requests.Session()
url = "https://twitter.com/login"
url_login = "https://twitter.com/sessions"
user_login = "user"
user_password = "pass"
user_agent = ("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36")
accept_language = 'ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4'
response = s.get(url)
posicion_token = response.text.find("authenticity_token")
print "El token es"
print response.text[posicion_token+27:posicion_token+67]
token = response.text[posicion_token+27:posicion_token+67]
login_post = {'session[username_or_email]' : user_login,
'session[password]' : user_password,
'return_to_ssl':'true',
'scribe_log':'',
'redirect_after_login':'%2F',
'authenticity_token':token
}
print login_post
s.headers.update ({'Accept-Encoding' : 'gzip, deflate',
'Accept-Language' : accept_language,
'Connection' : 'keep-alive',
'Content-type': 'application/x-www-form-urlencoded',
'Host' : 'www.twitter.com',
'Origin' : 'https://www.twitter.com',
'Referer' : 'https://www.twitter.com/',
'User-Agent' : user_agent
})
login = s.post(url_login, data=login_post, allow_redirects=True)
我得到的错误是
Traceback(最近一次调用最后一次):文件 " C:\ Users \ Usuario \ Desktop \ instabot-twitbot \ logueo_twitter.py",line 52,在 login = s.post(url_login,data = login_post,allow_redirects = True)文件" C:\ Python27 \ lib \ site-packages \ requests \ sessions.py",第555行, 在邮寄 return self.request(' POST',url,data = data,json = json,** kwargs)文件" C:\ Python27 \ lib \ site-packages \ requests \ sessions.py&# 34;,第508行, 在请求中 resp = self.send(prep,** send_kwargs)文件" C:\ Python27 \ lib \ site-packages \ requests \ sessions.py",第640行,in 发送 history = [resp for genp in gen] if allow_redirects else [] File" C:\ Python27 \ lib \ site-packages \ requests \ sessions.py",第140行,in resolve_redirects 引发TooManyRedirects('超过%s重定向。'%self.max_redirects,response = resp)TooManyRedirects:超过30 重定向。