Advertisement
furas

AliExpress API (requests, urllib, aliexpress_api_client)

May 21st, 2018
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. #
  4. # API doc: https://portals.aliexpress.com/help.htm?page=help_center_api
  5. #
  6. # Python wrapper: https://github.com/kronas/python-aliexpress-api-client
  7. # (There are some mistakes in example on GitHub)
  8. #
  9.  
  10. #----------------------------------------------------------------------
  11. # using standard modules: urllib, json
  12. #----------------------------------------------------------------------
  13.  
  14. import urllib.request, urllib.parse
  15. import json
  16.  
  17. API_KEY = '<api-key>'
  18. AFFILIANTE_ID = '<affiliate_id>'
  19.  
  20. url = 'https://gw.api.alibaba.com/openapi/param2/2/portals.open/api.listPromotionProduct/' + API_KEY
  21.  
  22. payload = {
  23.     'fields': ','.join(['productId', 'productTitle', 'salePrice']),
  24.     'keywords': 'chess',
  25.     'highQualityItems': 'yes',
  26. }
  27.  
  28. response = urllib.request.urlopen(url + '?' + urllib.parse.urlencode(payload))
  29. data = json.loads(response.read())
  30. #print(data)
  31. result = data['result']
  32. #print(result)
  33. products = result['products']
  34.  
  35. for product in products:
  36.     print('#%s %s: %s' % (product['productId'], product['productTitle'], product['salePrice']))
  37.  
  38. #----------------------------------------------------------------------
  39. # using external module: requests
  40. # $ pip install requests
  41. #----------------------------------------------------------------------
  42.  
  43. import requests
  44.  
  45. API_KEY = '<api-key>'
  46. AFFILIANTE_ID = '<affiliate_id>'
  47.  
  48. url = 'https://gw.api.alibaba.com/openapi/param2/2/portals.open/api.listPromotionProduct/' + API_KEY
  49.  
  50. payload = {
  51.     'fields': ','.join(['productId', 'productTitle', 'salePrice']),
  52.     'keywords': 'chess',
  53.     'highQualityItems': 'yes',
  54. }
  55.  
  56. response = requests.get(url, params=payload)
  57. #print(response.request.url)
  58. data = response.json()
  59. #print(data)
  60. result = data['result']
  61. #print(result)
  62. products = result['products']
  63.  
  64. for product in products:
  65.     print('#%s %s: %s' % (product['productId'], product['productTitle'], product['salePrice']))
  66.  
  67. # ---------------------------------------------------------------------
  68. # using external module: aliexpress_api_client
  69. # $ pip install aliexpress_api_client
  70. # or
  71. # $ git clone https://github.com/kronas/python-aliexpress-api-client
  72. # ---------------------------------------------------------------------
  73. # There are some mistakes in example on GitHub
  74. # ---------------------------------------------------------------------
  75.  
  76. from aliexpress_api_client import AliExpress
  77.  
  78. API_KEY = '<api-key>'
  79. AFFILIANTE_ID = '<affiliate_id>'
  80.  
  81. aliexpress = AliExpress(API_KEY, AFFILIANTE_ID)
  82.  
  83. fields = ['productId', 'productTitle', 'salePrice']
  84. keywords = 'chess'
  85.  
  86. result = aliexpress.get_product_list(fields, keywords)
  87. #print(result)
  88. products = result['products']
  89.  
  90. for product in products:
  91.     print('#%s %s: %s' % (product['productId'], product['productTitle'], product['salePrice']))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement