File size: 1,056 Bytes
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
41
42
43
44
import requests
import json
from dotenv import load_dotenv
import os

load_dotenv() 

username = os.environ.get('USERNAME')
passwd = os.environ.get('PASSWORD')
def scrape_amazon(search_query, n =10):
    # 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'],item['url'])
            i+=1
        else:
            break
        
    return final_result
print(scrape_amazon("yellow top"))