第一次在这里,我相当缺乏经验,很抱歉,如果我错过任何解释:)
我有一个可以在网站上创建帐户的脚本,它从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"
},
非常感谢:)
答案 0 :(得分:0)
不确定。只需将所有从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)
答案 1 :(得分:0)
是的,有一种方法可以设置脚本以便为多个用户运行。
首先让我们从组织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)
将返回一个字典列表,其中每个字典将是特定的配置。您需要遍历列表。之后,代码对于发出给定配置(用户配置)的请求是相同的。
希望这有帮助。