virtual-tryon-demo / amazon_oxy.py
cmahima's picture
changes for price and url
01b2828
raw
history blame
987 Bytes
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