第一次来这里,我没有经验,所以如果我错过了解释,我很抱歉:)
我有一个脚本,可以在一个网站上创建一个帐户,它从一个config.json文件中获得用户证书。唯一的问题是,它只能创建一个帐户每次运行。是否要设置此凭证以运行包含在config.json文件中的多个用户凭据?
“PY守则”如下:
import json
import requests
s = requests.Session()
headers = {
'Content-Type': 'application/json',
'X-API-Key': '--xxx--',
'Accept': '*',
'X-Debug': '1',
'User-Agent': 'FootPatrol/2.0 CFNetwork/808.2.16 Darwin/16.3.0',
'Accept-Encoding': 'gzip, deflate',
'MESH-Commcerce-Channel': 'iphone-app'
}
with open("config.json") as jsons:
config = json.load(jsons)
req = s.post("https://commerce.mesh.mx/stores/footpatrol/customers",
headers=headers, json=config)
print(req.text)config.json如下:
{
"phone": "07901893000",
"password": "passwprd3213",
"firstName": "Jon",
"gender": "",
"addresses": [
{
"locale": "gb",
"country": "United Kingdom",
"address1": "54 yellow Road",
"town": "Oxford",
"postcode": "OX1 1SW",
"isPrimaryBillingAddress": true,
"isPrimaryAddress": true
}
],
"title": "",
"email": "fdgsgfdg@gmail.com",
"isGuest": false,
"lastName": "Thomas"
},非常感谢:)
发布于 2017-10-26 21:33:10
是的,有一种方法可以设置脚本来为多个用户运行。
首先,让我们从组织config.json文件开始,该文件将包含所有用户的配置。该文件将是一个包含JSON对象的JSON数组。下面是文件结构的一个示例:
[
{
"phone": "07901893000",
"password": "passwprd3213",
"firstName": "Jon",
"gender": "",
"addresses": [
{
"locale": "gb",
"country": "United Kingdom",
"address1": "54 yellow Road",
"town": "Oxford",
"postcode": "OX1 1SW",
"isPrimaryBillingAddress": true,
"isPrimaryAddress": true
}
],
"title": "",
"email": "fdgsgfdg@gmail.com",
"isGuest": false,
"lastName": "Thomas"
},
{
"phone": "07901893000",
"password": "passwprd3213",
"firstName": "Mickey",
"gender": "",
"addresses": [
{
"locale": "gb",
"country": "USA",
"address1": "123 USA Road",
"town": "New Oxford",
"postcode": "OX1 1SW",
"isPrimaryBillingAddress": true,
"isPrimaryAddress": true
}
],
"title": "",
"email": "fdgsgfdg@gmail.com",
"isGuest": false,
"lastName": "Thomas"
}
]现在,让我们修改代码:
import json
import requests
s = requests.Session()
headers = {
'Content-Type': 'application/json',
'X-API-Key': '--xxx--',
'Accept': '*',
'X-Debug': '1',
'User-Agent': 'FootPatrol/2.0 CFNetwork/808.2.16 Darwin/16.3.0',
'Accept-Encoding': 'gzip, deflate',
'MESH-Commcerce-Channel': 'iphone-app'
}
with open("config.json") as jsons:
configs = json.load(jsons)
for config in configs:
req = s.post("https://commerce.mesh.mx/stores/footpatrol/customers",
headers=headers, json=config)
print(req.text)行configs = json.load(jsons)将返回字典列表,其中每个字典都是特定的配置。您需要遍历列表。在此之后,对于给定配置的请求(用户配置),代码是相同的。
希望这能有所帮助。
发布于 2017-10-26 21:24:58
好的。只需将从with开始的所有内容都放在一个循环中,这个循环可以遍历所有您告诉它要查找的文件。
files = ["config_1.json", "config_2.json", "config_3.json"]
for file in files:
with open(file) as jsons:
config = json.load(jsons)
req = s.post("https://commerce.mesh.mx/stores/footpatrol/customers",
headers=headers, json=config)
print(req.text)https://stackoverflow.com/questions/46963895
复制相似问题