2017-10-26 7 views
0

初めてのPythonスクリプト中の.jsonファイルを通って、私は何を解釈欠場場合、私はとても残念かなり経験の浅いよ:)ループ私はウェブサイト上でアカウントを作成することができ、スクリプトを持ってここに

、そのユーザーの資格情報を取得しますconfig.jsonファイルから取得します。唯一の問題は、実行ごとに1つのアカウントしか作成できないことです。このスクリプトを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": "[email protected]", 
      "isGuest": false, 
      "lastName": "Thomas" 
      }, 

多くの感謝:)

+0

APIキーを削除して新しいAPIキーを生成したい場合があります。 – voiDnyx

+0

こんにちは、APIキーは、常に同じままです。私は一度に200個のアカウントを作成しようとしています。問題は、一度に1つしか作成できない瞬間です。 –

+0

はい、あなたのAPIキーをここに掲示すると、あなたのキーがそれを使ってあなたの名前で悪いことをするのに使うことができます。 ;) – voiDnyx

答えて

0

はい、複数のユーザーに対して実行するようにスクリプトを設定する方法があります。

最初に、すべてのユーザーの設定を含むconfig.jsonファイルを整理することから始めましょう。ファイルは、JSONオブジェクトを含むJSON配列になります。各辞書は、特定のために設定されます辞書のリストを返します

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)

[ 
    { 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "isGuest": false, 
     "lastName": "Thomas" 
    } 
] 

は今、コードを変更することができます:ここでは、ファイル構造の例があります。あなたはリストを反復する必要があります。その後、指定されたconfig(ユーザーの設定)を要求するコードは同じです。

これが役に立ちます。

+0

あなたはすばらしいです - 夢のような作品!はじめて積み重ねを使用し、ここで寛大さを信じることができない(Y) –

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) 
+0

ありがとう、これは非常に便利です!設定ファイルにすべての詳細があるのですか?一意の詳細で200ファイルを作成するのに長い時間がかかります。 –

+0

もちろん、方法があります。しかし、この質問のタイトルは ".jsonファイルをループする"方法を尋ねるので、これが私が答えた質問です。ここの人々は、質問と回答の形式について非常に構造化されています。私はあなたがJSONファイルをdictsのリストにロードし、 'for'ループに各dictを引き出してリクエストに送るツール(私が投稿したものとあなたの既存のPythonの知識を与えられたもの)を持っているように感じます。 –

関連する問題