Spaces:
Running
on
Zero
Running
on
Zero
File size: 987 Bytes
43e8290 01b2828 43e8290 01b2828 43e8290 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import requests
import json
import os
username = os.environ.get('USERNAME')
passwd = os.environ.get('PASSWORD')
def scrape_amazon(search_query, n =20):
# Structure payload.
payload = {
'source': 'amazon_search',
'domain': 'com',
'query': f'{search_query}',
'start_page': 1,
'pages': 1,
'parse': True,
# 'context': [
# {'key': 'category_id', 'value': 16391693031}
# ],
}
# Get response.
response = requests.request(
'POST',
'https://realtime.oxylabs.io/v1/queries',
auth=(username, passwd),
json=payload,
)
result = json.loads(response.content)
final_result = {}
i=0
# Parse result.
for item in result['results'][0]['content']['results']['organic']:
if i<n:
final_result[item['url_image']]= (item['price'],"amazon.com"+item['url'])
i+=1
else:
break
return final_result
|