我正在使用以下代码对会话进行身份验证。然后,我想使用经过身份验证的会话来请求页面。这似乎适用于某些Google帐户,但对于某些无效。有人有更好的建议吗?
import requests
from BeautifulSoup import BeautifulSoup
def get_google_auth_session(username, password):
google_accounts_url = 'https://accounts.google.com'
authentication_url = 'https://accounts.google.com/ServiceLoginAuth'
session = requests.session()
user_agent = \
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
headers = {'User-Agent': user_agent}
login_html = session.get(google_accounts_url)
soup_login = BeautifulSoup(login_html.content, 'html.parser'
).find('form').find_all('input')
payload = {}
for u in soup_login:
if u.has_attr('value'):
payload[u['name']] = u['value']
payload['Email'] = username
payload['Passwd'] = password
auto = login_html.headers.get('X-Auto-Login')
follow_up = unquote(unquote(auto)).split('continue=')[-1]
payload['continue'] = follow_up
session.post(authentication_url, data=payload, headers=headers)
##this is where I want to use
##session.get(url)
get_google_auth_session(username='', password='')