นี่เป็น Python 3.7+ API อย่างไม่เป็นทางการสำหรับเว็บไซต์ pcpartpicker.com มันถูกเขียนโดยใช้คำขอแบบอะซิงโครนัสเพื่อการดึงข้อมูลที่มีประสิทธิภาพ แพ็คเกจนี้ปัจจุบันอยู่ในช่วงเบต้าที่เสถียร
การดึงและการติดตั้งแพ็คเกจสามารถทำได้ง่าย ๆ ผ่านทาง pip
pip install pcpartpicker
หากต้องการใช้ API เพียงนำเข้า API จากแพ็คเกจ pcpartpicker
from pcpartpicker import API
จากนั้นคุณสามารถยกตัวอย่างคลาส API และใช้มันเพื่อสร้างคำขอได้
api = API ()
cpu_data = api . retrieve ( "cpu" )
all_data = api . retrieve_all ()
วิธี api.retrieve()
และ api.retrieve_all()
ส่งคืนอินสแตนซ์ PartData
ซึ่งมีการประทับเวลาและวิธี to_json()
สามารถรับรายการชิ้นส่วนที่รองรับได้ในลักษณะต่อไปนี้:
api = API ()
print ( api . supported_parts )
>> > { 'wireless-network-card' , 'case-fan' , 'cpu' , 'cpu-cooler' , 'headphones' , 'motherboard' , 'monitor' , 'internal-hard-drive' , 'external-hard-drive' , 'ups' , 'fan-controller' , 'case' , 'keyboard' , 'mouse' , 'wired-network-card' , 'sound-card' , 'video-card' , 'speakers' , 'optical-drive' , 'power-supply' , 'thermal-paste' , 'memory' }
นอกจากนี้ยังมีวิธีการอีกหลายวิธีที่สามารถใช้เพื่อปรับแต่งพฤติกรรมของ API ได้ ตัวอย่างเช่น คุณสามารถเปลี่ยนภูมิภาคและกำหนดจำนวนการเชื่อมต่อแบบอะซิงโครนัสที่เกิดขึ้นพร้อมกันที่สามารถทำได้
การดึงข้อมูลขอบเขต API ที่รองรับ:
api = API ()
print ( api . supported_regions )
>> > { 'be' , 'us' , 'it' , 'uk' , 'ie' , 'nz' , 'de' , 'ca' , 'au' , 'fr' , 'se' , 'es' , 'in' }
กำลังดึงข้อมูลภูมิภาคที่เลือกในปัจจุบัน (ค่าเริ่มต้นคือ US):
api = API ()
print ( api . region )
>> > us
การสร้างออบเจ็กต์ API ด้วยขอบเขตเริ่มต้นอื่น:
api = API ( "de" )
print ( api . region )
>> > de
การเปลี่ยนภูมิภาคเริ่มต้น:
api = API ()
api . set_region ( "de" )
print ( api . region )
>> > de