Skip to content

Commit ae9ee65

Browse files
committed
API
1 parent 0259b6d commit ae9ee65

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

API/shopee.sg API/main.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
# author: Bartlomiej "furas" Burek (https://blog.furas.pl)
3+
# date: 2021.07.22
4+
#
5+
# title: How to use loops to scrape data from API
6+
# url: https://stackoverflow.com/questions/68478524/how-to-use-loops-to-scrape-data-from-api/68479878?noredirect=1#comment121025287_68479878
7+
8+
import requests
9+
10+
# --- functions ---
11+
12+
def main(search_term, items_number=250):
13+
14+
url = "https://shopee.sg/api/v4/search/search_items"
15+
16+
params = {
17+
"by": "relevancy",
18+
"categoryids": 100630,
19+
"keyword": search_term,
20+
"limit": 100,
21+
"newest": 0, # default value at start
22+
"order": "desc",
23+
"page_type": "search",
24+
"scenario": "PAGE_GLOBAL_SEARCH",
25+
"version": "2"
26+
}
27+
28+
for offset in range(0, items_number, 100):
29+
30+
print('\n--- offset:', offset, '---\n')
31+
32+
params["newest"] = offset # other parameters are the same
33+
34+
limit = items_number - offset
35+
if limit < 100:
36+
params["limit"] = limit
37+
38+
r = requests.get(url, params=params)
39+
data = r.json()
40+
41+
print('>> items number in data:', len(data['items']), '\n')
42+
43+
for number, item in enumerate(data['items']):
44+
45+
print('number:', offset+number+1)
46+
47+
basic = item['item_basic']
48+
49+
#print('keys:', basic.keys())
50+
#for key, value in basic.items():
51+
# print(f"{key}: {value}")
52+
53+
# NAME
54+
name = basic['name']
55+
#name = basic.get('name', '- unknown -') # safer if `name` may not exists in `data`
56+
print("Name:", name)
57+
58+
# BRAND
59+
#brand = basic['brand']
60+
brand = basic.get('brand') # safer if `brand` may not exists in `data`
61+
if brand is None:
62+
print()
63+
else:
64+
print("Brand:", brand)
65+
66+
print('---')
67+
68+
# --- main ---
69+
70+
main("make-up")
71+
#main("make-up", 60)
72+
#main("make-up", 270)

API/tenor.com API/main.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import requests
2+
3+
# set the apikey and limit
4+
API_KEY = "LIVDSRZULELA" # test value
5+
search_term = "cat"
6+
7+
def get_urls(search, limit=8):
8+
payload = {
9+
'key': API_KEY,
10+
'limit': limit,
11+
'q': search,
12+
}
13+
# our test search
14+
15+
# get the top 8 GIFs for the search term
16+
r = requests.get("https://g.tenor.com/v1/search", params=payload)
17+
18+
results = []
19+
20+
if r.status_code == 200:
21+
data = r.json()
22+
#print('[DEBUG] data:', data)
23+
24+
for item in data['results']:
25+
#print('[DEBUG] item:', item)
26+
for media in item['media']:
27+
#print('[DEBUG] media:', media)
28+
#for key, value in media.items():
29+
# print(f'{key:10}:', value['url'])
30+
#print('----')
31+
32+
if 'tinygif' in media:
33+
results.append(media['tinygif']['url'])
34+
else:
35+
results = []
36+
37+
return results
38+
39+
# --- main ---
40+
41+
cat_encouragements = get_urls('cat')
42+
43+
for url in cat_encouragements:
44+
print(url)
45+
46+

0 commit comments

Comments
 (0)