 |
[python] Call API วิธีเขียน loop meta next ใน Python อย่างไร ติดปัญหา loop meta next เจอค่า Null ครับ |
|
 |
|
|
 |
 |
|
Quote:Call API วิธีเขียน loop meta next ใน Python อย่างไร ติดปัญหา loop meta next เจอค่า Null ครับ
แบ่งเป็น code 2 ชุดครับ (เป็นรูปภาพครับและลง code ครับ)
1. call api status 200
2. เอาค่า meta next ไป loop ต่อ status 200 แต่ติดเจอค่า null ครับ ทั้งๆที่มีข้อมูลอยู่ครับ

Code (Python call api CODE1)
import requests
import json
url = "https://api.threatstream.com/api/v2/intelligence/?&q=xxxx"
# api_key ="apikey"
# user = "user"
headers = {
"Content-Type": "application/json",
"Authorization": "apikey " + user + ":" + api_key
}
response = requests.get(url, verify=False, headers=headers)
data = response.json()
print(json.dumps(data, indent=4))
if (response.status_code == 200):
print("status_code = " + str(response.status_code) + " The request was a success!")
elif (response.status_code == 404):
print("status_code = " + str(response.status_code) + " Result not found!")
Code (Response call api CODE1)
"meta": {
"total_count": 1201,
"offset": 0,
"limit": 1000,
"took": 174,
"next": "/api/v2/intelligence/?q=%28%28status%3D%27active%27%29+and+%28confidence+%3E%3D+100%29+and+%28country+%3D+TH%29+or+%28country+%3D+AD%29%29&search_after=1480970530887%2C292500861&limit=1000"
}
}
status_code = 200 The request was a success!
Code (Python loop meta next CODE2)
import requests
import json
# base url
base_url = 'https://api.threatstream.com'
api_key ="apikey"
user = "user"
headers = {
"Content-Type": "application/json",
"Authorization": "apikey " + user + ":" + api_key
}
# Query data
next = "/api/v2/intelligence/?q=%28%28status%3D%27active%27%29+and+%28confidence+%3E%3D+100%29+and+%28country+%3D+TH%29+or+%28country+%3D+AD%29%29&search_after=1452851400110%2C47783443&limit=1000"
while True:
final_url = base_url + next
response = requests.get(final_url, headers=headers, verify=False)
data = response.json()
print(json.dumps( data, indent=4))
with open('get.json', 'w') as json_file:
json.dump(data, json_file, indent=4)
if (response.status_code == 200):
print("status_code = " + str(response.status_code) + " The request was a success!")
elif (response.status_code == 404):
print("status_code = " + str(response.status_code) + " Result not found!")
Code (Response loop meta next CODE2)
"meta": {
"total_count": 1201,
"offset": 0,
"limit": 1000,
"took": 136,
"next": null
}
}
status_code = 200 The request was a success!
Tag : Python
|
ประวัติการแก้ไข 2022-05-05 13:45:29 2022-05-05 13:48:50 2022-05-05 13:49:46
|
 |
 |
 |
 |
Date :
2022-05-05 13:40:16 |
By :
TeeTs |
View :
1132 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
น่าจะต้องแก้ link ใน next ให้ถูกต้องครับ ไม่น่าจะต้องใช้ url encode ให้ใส่ แบบที่คนอ่านได้
ตัว python จะแปลงเอง คุณไปแปลงให้ มันอาจจะแปลง ซ้ำซ้อน ทำให้หา ลิงค์ ไม่เจอ หรือเปล่า
ผมแค่ไม่เคยใส่ลิงค์แบบนี้
ปล. python ผมแค่ งูปลาๆ นะ 5555
|
 |
 |
 |
 |
Date :
2022-05-05 15:18:33 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 1 เขียนโดย : Chaidhanan เมื่อวันที่ 2022-05-05 15:18:33
รายละเอียดของการตอบ ::
ครับพี่ Chaidhanan
API จะใช้แบบนี้ครับ
1. เมื่อ call api มาได้
2. หลังจาก call api มาได้ ให้นำ meta next ส่วนนี้ไปทำ loop parameter ต่อครับ
ซึ่งได้ทำตามที่แนะนำ แต่เจอค่า null ครับ
ส่วนที่พี่แนะนำว่า ต้องแก้ link ใน next ให้ถูกต้องครับ ให้คนอ่านได้ ตัว python จะแปลงเอง อันนี้ใช่ครับ ได้ลองแล้วครับ สามารถ loop ได้ครับ
ปัญหาคือ loop เรื่อยๆครับเป็นข้อมูลเดิมครับ เช่น
- loop 1 มี total_count 1201 (loop 1000 limit)
- loop 2 มี total_count 1201 ( loop เรื่อยๆครับ ซึ่งยังไม่ถูก เพราะมี total_count แค่ 1201)
( พี่มีวิธี python เขียน loop meta next pages ช่วยแนะนำด้วยครับ )
ส่วนหลักการ loop เข้าใจครับ ว่าต้องเป็นแบบนี้
- loop 1 มี total_count 1201 (loop 1000 limit) คือได้ข้อมูลมา 1000 ชุดแล้ว เหลืออีก 201 ข้อมูล ก็ต้อง loop ต่อเป็น loop2
- loop 2 loop 1000 limit ต่อจาก loop 1 ต้องได้ข้อมูล 201 จนครบและเจอค่า null เมื่อไม่มีข้อมูลครับ หากอธิบายผิดพลาดอภัยด้วยครับ
|
ประวัติการแก้ไข 2022-05-05 15:46:59
 |
 |
 |
 |
Date :
2022-05-05 15:43:33 |
By :
TeeTs |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ฝั่ง api เขียนเองหรือเปล่าครับ
ถ้าเขียนเอง ก็ให้แก้เพิ่มเติม กำหนด ตำแหน่ง เริ่มต้น สิ้นสุดได้ ด้วย
พอ loop ครั้งต่อไป ก้กำหนด ตำแหน่งเริ่มต้นใหม่ ครับ
แต่ถ้าไม่ได้ เขียนเอง ก็ต้องถาม ในส่วนของ คนเขียน api ครับว่าจะเรียกใช้ยังไง
|
 |
 |
 |
 |
Date :
2022-05-05 16:20:01 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Quote:ทำได้ ข้อ1-3
และ ข้อ 4-6 ขอคำแนะนำด้วยครับ
process
1. สร้างเก็บ base url
2. นำ base url ไปต่อกับ path ที่จะ query
3. request ไปครั้งแรกโดยใช้ url ที่ได้จาก ข้อ.2 มา
4.นำ response ที่ได้ไปเช็ค key next เป็น null หรือไม่
- ถ้า key next เป็น null หมายความว่าดึงข้อมูลมาหมดแล้ว ไม่ต้องทำอะไรต่อ
- ถ้า key next return path ออกมาให้ทำขั้นตอนถัดไป
5. นำ path ที่ได้จากข้อ 4 ไปต่อกับ base url ของ ข้อ 1
6. request โดยใช้ url จากข้อ 5 และวนกลับไปทำข้อ 4
Code (Python 1-3)
from asyncio.windows_events import NULL
from typing import final
import requests
import json
# 1
host_url = "https://api.xxxxx.com"
api_key ="apikey"
user = "user"
# 2
query = "/api/v2/intelligence/?&q=xxxx" # q ที่อ่านได้
path = host_url+query
headers = {
"Content-Type": "application/json",
"Authorization": "apikey " + user + ":" + api_key,
}
# 3
response = requests.get(path, headers=headers, verify=False)
data = response.json()
print(json.dumps(data ,indent=4))
with open('IOC.json', 'w') as json_file:
json.dump(data, json_file ,indent=4)
print(response.status_code)
Code (Response)
"meta": {
"total_count": 1642,
"offset": 0,
"limit": 1000,
"took": 268,
"next": "/api/v2/intelligence/?q=xxxx" # q ที่อ่านไม่ได้
}
200
|
ประวัติการแก้ไข 2022-05-13 09:21:45
 |
 |
 |
 |
Date :
2022-05-13 09:14:00 |
By :
TeeTs |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|