Files
004_comission/Man1130/jupyter/jupyter-helloworld/Search.ipynb
louiscklaw fc6f79b133 update,
2025-01-31 20:57:47 +08:00

157 lines
4.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import copy\n",
"import requests\n",
"import pprint"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"searchURL = 'https://www.carousell.sg/api-service/filter/cf/4.0/search/'\n",
"searchContent = {\n",
" 'bestMatchEnabled': False,\n",
" 'canChangeKeyword': False,\n",
" 'count': 40, # maximum appears to be 40 results\n",
" 'countryCode': 'SG',\n",
" 'countryId': '1880251',\n",
" #'ccid': None, # filter by category (must specify collection ID under filters too)\n",
" 'filters': [\n",
" # {'fieldName': 'price', 'rangedFloat': {'end': {'value': None}, 'start': {'value': None}}} # filter by price\n",
" # {'fieldName': 'collections', 'idsOrKeywords': {'value': None}} # filter by category (must specify correct ccid too)\n",
" ], \n",
" 'includeSuggestions': False,\n",
" 'locale': 'en',\n",
" 'prefill': [], # Seems to be used when adding filters to an existing search (not used by us)\n",
" 'query': None,\n",
" 'sortParam': { # used to specify: sorting of returned results (currently set to return most recent listings)\n",
" 'fieldName': 'time_created', \n",
" 'ascending': {\n",
" 'value': False\n",
" }\n",
" }\n",
"}\n",
"\n",
"\n",
"# additional search content for various searches\n",
"# for filtering by price\n",
" # {'rangedFloat': {'end': {'value': 'max'}, 'start': {'value': 'min'}}, 'fieldName': 'price'}\n",
"# for filtering by category\n",
" # ccid: (2196 = Desktops)\n",
" # filters\n",
" # {'fieldName': 'collections', 'idsOrKeywords': {'value': '1794'}} - for Desktops\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def query(keyword = None, category = None, minPrice = None, maxPrice = None):\n",
" # construct POST content\n",
" content = copy.deepcopy(searchContent)\n",
" if keyword != None:\n",
" print('Keyword: ' + keyword)\n",
" content['query'] = keyword\n",
" if category != None:\n",
" print('Category: ' + category)\n",
" if minPrice != None or maxPrice != None:\n",
" content['filters'].append({'fieldName': 'price', 'rangedFloat': {}})\n",
" if minPrice != None:\n",
" minPriceStr = str(minPrice)\n",
" print('Minimum price: ' + minPriceStr)\n",
" content['filters'][0]['rangedFloat']['start'] = {'value': minPriceStr}\n",
" if maxPrice != None:\n",
" maxPriceStr = str(maxPrice)\n",
" print('Maximum price: ' + maxPriceStr)\n",
" content['filters'][0]['rangedFloat']['end'] = {'value': maxPriceStr}\n",
" \n",
" pprint.pprint({\"searchURL\":searchURL})\n",
" pprint.pprint(content)\n",
" # perform POST search, retrieve JSON response\n",
" \n",
" \n",
"# searchResp = requests.post(url=searchURL, json=content)\n",
"# searchRespData = searchResp.json()\n",
"# print(searchRespData)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Keyword: gtx1060\n",
"Minimum price: 300\n",
"Maximum price: 400\n",
"{'searchURL': 'https://www.carousell.sg/api-service/filter/cf/4.0/search/'}\n",
"{'bestMatchEnabled': False,\n",
" 'canChangeKeyword': False,\n",
" 'count': 40,\n",
" 'countryCode': 'SG',\n",
" 'countryId': '1880251',\n",
" 'filters': [{'fieldName': 'price',\n",
" 'rangedFloat': {'end': {'value': '400'},\n",
" 'start': {'value': '300'}}}],\n",
" 'includeSuggestions': False,\n",
" 'locale': 'en',\n",
" 'prefill': [],\n",
" 'query': 'gtx1060',\n",
" 'sortParam': {'ascending': {'value': False}, 'fieldName': 'time_created'}}\n"
]
}
],
"source": [
"query(keyword=\"gtx1060\", minPrice=300, maxPrice=400)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
},
"metadata": {
"interpreter": {
"hash": "22f70f503a14852dda6f3675b02ae61e38835edc56b87f63457d6e30f459ac44"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}