Google 脚本是否与 python 的 Session object 等效?
Does Google Script have an equivalent to python's Session object?
我有这个 python 脚本,我想获得 Google 等效脚本,但我不知道如何“传递”下一个 get 或 post 之间需要传递的任何内容登录后请求。
import requests
import json
# login
session = requests.session()
data = {
'LoginName': 'name',
'Password': 'password'
}
session.post('https://www.web.com/en-CA/Login/Login', data=data)
session.get('https://www.web.com//en-CA/Redirect/?page=Dashboard')
# get customer table
data = {
'page': '1',
'pageSize': '100'
}
response = session.post('https://www.web.com/en-CA/Reporting', data=data)
print(response.json())
我想知道 python 的请求模块中是否有等同于 .session()
object 的模块。我确实搜索了 google 但找不到任何可用的示例。我不是编码员,所以我完全知道 .session() object 确实如此。发出新请求时从响应中传递 headers 就足够了吗?
更新
我在其他一些问题中读到 Google 可能正在为每个 UrlFetchApp.fetch
不同的 IP 使用,所以我猜登录和 cookie 可能不起作用。
我相信你的目标如下。
- 您想使用 Google Apps 脚本实现您的 python 脚本。
问题和解决方法:
如果我的理解是正确的,当使用session()
of python时,可以通过保留cookie来实现多次请求。例如,为了使用 Google Apps 脚本实现这种情况,我认为在第一次请求时检索到 cookie,并且检索到的 cookie 包含在第二次请求的请求 header 中。因为,现阶段UrlFetchApp
没有办法直接保存cookie,下次请求使用
从上面的情况来看,当你的脚本转换为Google Apps Script后,就变成了下面这样。
示例脚本:
function myFunction() {
const url1 = "https://www.web.com/en-CA/Login/Login";
const url2 = "https://www.web.com//en-CA/Redirect/?page=Dashboard";
const url3 = "https://www.web.com/en-CA/Reporting";
// 1st request
const params1 = {
method: "post",
payload: {LoginName: "name", Password: "password"},
followRedirects: false
}
const res1 = UrlFetchApp.fetch(url1, params1);
const headers1 = res1.getAllHeaders();
if (!headers1["Set-Cookie"]) throw new Error("No cookie");
// 2nd request
const params2 = {
headers: {Cookie: JSON.stringify(headers1["Set-Cookie"])},
followRedirects: false
};
const res2 = UrlFetchApp.fetch(url2, params2);
const headers2 = res2.getAllHeaders();
// 3rd request
const params3 = {
method: "post",
payload: {page: "1", pageSize: "100"},
headers: {Cookie: JSON.stringify(headers2["Set-Cookie"] ? headers2["Set-Cookie"] : headers1["Set-Cookie"])},
followRedirects: false
}
const res3 = UrlFetchApp.fetch(url3, params3);
console.log(res3.getContentText())
}
- 通过此示例脚本,可以从第一个请求中检索 cookie,并且检索到的 cookie 可用于下一个请求。
- 很遗憾,我没有您实际服务器的信息,也无法测试您的实际 URL。所以我不确定这个示例脚本是否直接适用于您的服务器。
- 而且,我不确定每个请求中是否需要包含
followRedirects: false
。所以当出现错误时,请将其移除并重新测试。
- 关于将 cookie 包含到请求中的方法 header,可能不需要使用
JSON.stringify
。但是,对于您的服务器,我不确定这一点。
参考:
您可能想试试这个:
var nl = getNewLine()
function getNewLine() {
var agent = navigator.userAgent
if (agent.indexOf("Win") >= 0)
return "\r\n"
else
if (agent.indexOf("Mac") >= 0)
return "\r"
return "\r"
}
pagecode = 'import requests
import json
# login
session = requests.session()
data = {
\'LoginName\': \'name\',
\'Password\': \'password\'
}
session.post(\'https://www.web.com/en-CA/Login/Login\', data=data)
session.get(\'https://www.web.com//en-CA/Redirect/?page=Dashboard\')
# get customer table
data = {
\'page\': \'1\',
\'pageSize\': \'100\'
}
response = session.post(\'https://www.web.com/en-CA/Reporting\', data=data)
print(response.json())'
document.write(pagecode);
我用了this program
我有这个 python 脚本,我想获得 Google 等效脚本,但我不知道如何“传递”下一个 get 或 post 之间需要传递的任何内容登录后请求。
import requests
import json
# login
session = requests.session()
data = {
'LoginName': 'name',
'Password': 'password'
}
session.post('https://www.web.com/en-CA/Login/Login', data=data)
session.get('https://www.web.com//en-CA/Redirect/?page=Dashboard')
# get customer table
data = {
'page': '1',
'pageSize': '100'
}
response = session.post('https://www.web.com/en-CA/Reporting', data=data)
print(response.json())
我想知道 python 的请求模块中是否有等同于 .session()
object 的模块。我确实搜索了 google 但找不到任何可用的示例。我不是编码员,所以我完全知道 .session() object 确实如此。发出新请求时从响应中传递 headers 就足够了吗?
更新
我在其他一些问题中读到 Google 可能正在为每个 UrlFetchApp.fetch
不同的 IP 使用,所以我猜登录和 cookie 可能不起作用。
我相信你的目标如下。
- 您想使用 Google Apps 脚本实现您的 python 脚本。
问题和解决方法:
如果我的理解是正确的,当使用session()
of python时,可以通过保留cookie来实现多次请求。例如,为了使用 Google Apps 脚本实现这种情况,我认为在第一次请求时检索到 cookie,并且检索到的 cookie 包含在第二次请求的请求 header 中。因为,现阶段UrlFetchApp
没有办法直接保存cookie,下次请求使用
从上面的情况来看,当你的脚本转换为Google Apps Script后,就变成了下面这样。
示例脚本:
function myFunction() {
const url1 = "https://www.web.com/en-CA/Login/Login";
const url2 = "https://www.web.com//en-CA/Redirect/?page=Dashboard";
const url3 = "https://www.web.com/en-CA/Reporting";
// 1st request
const params1 = {
method: "post",
payload: {LoginName: "name", Password: "password"},
followRedirects: false
}
const res1 = UrlFetchApp.fetch(url1, params1);
const headers1 = res1.getAllHeaders();
if (!headers1["Set-Cookie"]) throw new Error("No cookie");
// 2nd request
const params2 = {
headers: {Cookie: JSON.stringify(headers1["Set-Cookie"])},
followRedirects: false
};
const res2 = UrlFetchApp.fetch(url2, params2);
const headers2 = res2.getAllHeaders();
// 3rd request
const params3 = {
method: "post",
payload: {page: "1", pageSize: "100"},
headers: {Cookie: JSON.stringify(headers2["Set-Cookie"] ? headers2["Set-Cookie"] : headers1["Set-Cookie"])},
followRedirects: false
}
const res3 = UrlFetchApp.fetch(url3, params3);
console.log(res3.getContentText())
}
- 通过此示例脚本,可以从第一个请求中检索 cookie,并且检索到的 cookie 可用于下一个请求。
- 很遗憾,我没有您实际服务器的信息,也无法测试您的实际 URL。所以我不确定这个示例脚本是否直接适用于您的服务器。
- 而且,我不确定每个请求中是否需要包含
followRedirects: false
。所以当出现错误时,请将其移除并重新测试。 - 关于将 cookie 包含到请求中的方法 header,可能不需要使用
JSON.stringify
。但是,对于您的服务器,我不确定这一点。
参考:
您可能想试试这个:
var nl = getNewLine()
function getNewLine() {
var agent = navigator.userAgent
if (agent.indexOf("Win") >= 0)
return "\r\n"
else
if (agent.indexOf("Mac") >= 0)
return "\r"
return "\r"
}
pagecode = 'import requests
import json
# login
session = requests.session()
data = {
\'LoginName\': \'name\',
\'Password\': \'password\'
}
session.post(\'https://www.web.com/en-CA/Login/Login\', data=data)
session.get(\'https://www.web.com//en-CA/Redirect/?page=Dashboard\')
# get customer table
data = {
\'page\': \'1\',
\'pageSize\': \'100\'
}
response = session.post(\'https://www.web.com/en-CA/Reporting\', data=data)
print(response.json())'
document.write(pagecode);
我用了this program