Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python请求登录google以检索登录页面的html?_Python_Cookies - Fatal编程技术网

如何使用python请求登录google以检索登录页面的html?

如何使用python请求登录google以检索登录页面的html?,python,cookies,Python,Cookies,我正在尝试创建一个Python脚本,其中给定用户名和密码,脚本将登录到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 = {

我正在尝试创建一个Python脚本,其中给定
用户名
密码
,脚本将登录到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
文件时,它不会显示我已登录,因为有一个“登录”按钮

我打印了cookie,如果我在
get
http://google.com/
,当我使用
用户代理
标题时,会出现
缺口
cookie

我从登录页面的html格式中获得了
电子邮件
Passwd


有人知道如何解决这个问题吗?

为什么使用请求模块,而你有谷歌api。@KarthikeyanKR当用户登录时,谷歌api是否允许你获取网页的html,例如Gmail?为什么使用请求模块,而你有谷歌api。@KarthikeyanKR当用户登录时,谷歌api是否允许你获取网页的html,例如Gmail?