|
 |
ปรึกษาเรื่อง Python get api url ตรง api url จำกัด limit ได้แค่ 1000 limit จะใช้ loop parameter next ได้อย่างไร |
|
 |
|
|
 |
 |
|
***กรณี next ไม่ใช่ตัวเลขหน้า ที่คล้ายๆ page ครับ***
1. ยิง URL ได้ข้อมูลกลับมาทีละ 1000 อัน คือ ข้อมูลที่ 1-1000 ( api url จำกัด limit ได้แค่ 1000)
2. ถ้าใส่ Parameter next ไปใน URL ด้วย ก็ได้มา 1000 อัน แต่จะเริ่มต้นที่อันที่ 1001 (ข้อมูลที่ 1001-2000 จะloop ได้ 1000 เหมือนกัน)
3. ขอวิธีตัวอย่าง loop parameter next ครับ (ไม่ใช่ page =1,2,3)
4.code
Code (Python)
import requests
import json
url = "https://api.xxx.com/"
api_key ="test"
user = "[email protected]"
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!")
5.response code เยอะหน่อย เอาตรง meta มาให้ครับ
Code (response)
"meta": {
"total_count": 2234,
"offset": 0,
"limit": 1000,
"took": 138,
"next": "/api/v2/intelligence/?q=%28%28status%3D%27active%27%29+and+%28confidence%3E%3D90%29+and+%28country%3D%27TH%27%29%29&search_after=1628780720222%2C57395475643&limit=1000"
}
Tag : Python
|
|
 |
 |
 |
 |
Date :
2021-09-06 18:38:54 |
By :
TeeTs |
View :
753 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เทคนิคที่ผมใช้ คล้ายกับ next page แต่ไม่ใช้ next page =1,2,3 เอามาปรับใช้แล้ว error help me ple.
Code (Python)
import requests
import json
# url = "www.api.xxx.com"
api_key ="test"
user = "[email protected]"
headers = {
"Content-Type": "application/json",
"Authorization": "apikey " + user + ":" + api_key,
}
# 2. ใช้เทคนิคแยกข้อความ ใช้เป็น URL
# link API ตั้งต้น
base_url = "https://api.threatstream.com/api/v2/intelligence/?&q=((status='active') and (confidence>=90) and (country='TH'))"
# link Query ข้อมูล เอาจาก next json
next_path = "/api/v2/intelligence/?q=%28%28status%3D%27active%27%29+and+%28confidence%3E%3D90%29+and+%28country%3D%27TH%27%29%29&search_after=1630461816628%2C57480266984&limit=1000"
for i in range(1, 3):
# ปิด url
# url = "https://api.threatstream.com/api/v2/intelligence/?&q=((status='active') and (confidence>=90) and (country='TH'))"
# ใน loop ทุกรอบ ให้สร้าง url ที่ใช้ดึงข้อมูลทีละชุด
full_url = base_url + next_path
response = requests.get(full_url, headers=headers, verify=False)
response_json = response.json()
# 1. ดึงค่า next ออกมาจาก response_json
response_json['meta']['next']
print(json.dumps(response_json, indent=2))
|
 |
 |
 |
 |
Date :
2021-09-06 19:02:52 |
By :
TeeTs |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|