我正在尝试创建一个Python脚本,其中给定username
和password
,脚本登录到Google,然后在登录时获取页面(例如Google主页)的HTML数据。你知道吗
我的代码不工作,但我不知道为什么。你知道吗
import sys
reload(sys)
sys.setdefaultencoding('utf-8') # need to do this to be able to write html data to a file
from requests import session
payload = {
'Email': EMAIL,
'Passwd': PASSWORD
}
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} # to make Google think that this is a real browser not a python script
with session() as c:
res1 = c.post('https://accounts.google.com/signin/challenge/sl/password', data=payload, headers=headers)
res2 = c.get('http://www.google.com/', headers=headers)
html = res2.text
f = open("test.html", "w")
f.write(html)
f.close()
当我进入我编写的html
文件时,它不会显示我已登录,因为有一个“登录”按钮。你知道吗
我打印出cookies,如果我不使用User-Agent
头,当get
http://google.com/
时,会有一个“NID”cookie,当我使用User-Agent
头时,会有一个GAPS
cookie。你知道吗
我从登录页面的html格式获得了Email
和Passwd
。你知道吗
有人知道怎么解决这个问题吗?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐