1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| import requests import json
url = 'https://jsonplaceholder.typicode.com/posts' response = requests.get(url) posts = response.json()
filtered_posts = [post for post in posts if post['id'] in [1, 2]]
""" # Jsonplaceholderから取得したデータ filtered_posts = [ {'userId': 1, 'id': 1, 'title': 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', 'body': 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto'}, {'userId': 1, 'id': 2, 'title': 'qui est esse', 'body': 'est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla'} ] """
API_TOKEN = 'xxxxxxxxxxxxxx' APP_ID = 'x' DOMAIN = 'xxxxxxxxxxxxxx.cybozu.com' API_URL = f'https://{DOMAIN}/k/v1/record.json'
headers = { "X-Cybozu-API-Token": API_TOKEN, "Content-Type": "application/json" }
for post in filtered_posts: table_data = { "table": { "value": [ { "value": { "userId": {"value": post["userId"]}, "id": {"value": post["id"]}, "title": {"value": post["title"]}, "body": {"value": post["body"]} } } ] } } record_data = { "app": APP_ID, "record": table_data } response = requests.post(API_URL, headers=headers, data=json.dumps(record_data)) if response.status_code == 200: print(f"Post ID {post['id']} successfully registered.") else: print(f"Failed to register post ID {post['id']}. Status code: {response.status_code}, Response: {response.text}") print(f"Failed to register: {record}. Status code: {response.status_code}, Response: {response.text}")
|