forked from theonlyanil/amzpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage_examples.py
More file actions
240 lines (187 loc) · 10.2 KB
/
usage_examples.py
File metadata and controls
240 lines (187 loc) · 10.2 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python3
"""
AmzPy Advanced Usage Examples
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This example script demonstrates the enhanced features of AmzPy 0.2.0 including:
- Session configuration with curl_cffi
- Product details extraction
- Product searching with pagination
- Configuration options
- Enhanced search result parsing with color variants, discounts, and badges
Usage:
python usage_curl_cffi.py
"""
from amzpy import AmazonScraper
import json
from pprint import pprint
def print_section(title):
"""Print a section header for cleaner output"""
print("\n" + "="*80)
print(f" {title} ".center(80, "="))
print("="*80 + "\n")
def example_product_detail():
"""Example: Fetching product details"""
print_section("Product Details Example")
# Create a basic scraper for Amazon India
scraper = AmazonScraper(country_code="in")
# Configure scraper with custom settings
scraper.config('MAX_RETRIES = 3, REQUEST_TIMEOUT = 30')
# Set a sample product URL
# url = "https://www.amazon.in/Sparx-Mens-Red-Black-Flip-Flops/dp/B00IZ936HM/ref=sr_1_37?dib=eyJ2IjoiMSJ9.4xTlnBMWRsZUhc76GhsEgupJp7scFI1-LxWAaKnsBdUkKSFcwS5srU4Asgjnk3U73l841VhqU6DuRbOXQK7_HJ7UufcibD4L1WLcwEZSgmhSxgH--aqN5mp9fjrXRLbbPUIKKcqRN4KpPR-NEmcbGzKqpcSYKgAB9xfCEthgOjpjStXo7LLfKg5vuYALFuJjGiWgcHF3nJOXar4AHCRgSMFtiqQhAQmV_sr_c6rPKtk5YoGq1vn3SuaTNg6FeJiNOw5zNpavF7yS9zUFA0WU0BVve2y09CRdMw18fEqDQJk.zvtFkTh7TwTuQDwW4qBzr63RniJyBBeZjiDg86FATaM&dib_tag=se&qid=1744987780&refinements=p_6%3AA1WYWER0W24N8S%7CAWRHVFFS77KOB&rnid=1318474031&s=shoes&sr=1-37"
url = "https://www.amazon.in/Ant-Esports-MK801-V2-Mechanical/dp/B0DX1MF624/?_encoding=UTF8&pd_rd_w=gBV9F&content-id=amzn1.sym.3dbb2a14-4841-44d7-82b9-38efdf8e22f7&pf_rd_p=3dbb2a14-4841-44d7-82b9-38efdf8e22f7&pf_rd_r=PE7J86TCWM1CH4HJ03MH&pd_rd_wg=cu00m&pd_rd_r=8228a307-3435-4bbf-81e7-c8809290880f&ref_=pd_hp_d_atf_dealz_m2&th=1"
# Fetch and display product details
print(f"Fetching details for: {url}\n")
product = scraper.get_product_details(url)
if product:
print("\nProduct details successfully fetched:")
# Display in a more organized format
print(f"ASIN: {product.get('asin')}")
print(f"Title: {product.get('title')}")
print(f"Brand: {product.get('brand')}")
print(f"Price: {product.get('currency', '')} {product.get('price')}")
print(f"Rating: {product.get('rating')} / 5.0")
print(f"Image URL: {product.get('img_url')}")
print(f"Product URL: {product.get('url')}")
# Save to JSON file for reference
with open(f"product_{product.get('asin')}.json", "w") as f:
json.dump(product, f, indent=2)
print(f"\nFull details saved to product_{product.get('asin')}.json")
else:
print("Failed to fetch product details.")
def example_search_by_query(query: str = None):
"""Example: Searching for products with enhanced parsing"""
print_section("Enhanced Product Search Example")
# Create a scraper with configuration
scraper = AmazonScraper(
country_code="in",
impersonate="chrome119"
)
# Method 1: Search for shoes on Amazon India
print("\n--- Method 1: Enhanced Search by Keyword ---")
print(f"Searching for: '{query}' on Amazon India")
# Search with 5 pages of results for demonstration
products = scraper.search_products(query=query, max_pages=5)
# Display the enhanced results
if products:
print(f"\nFound {len(products)} products:")
# Show more detailed information for the first few products
detailed_count = min(3, len(products))
for i, product in enumerate(products[:detailed_count], 1):
print(f"\n{i}. {product.get('title', 'N/A')}")
print(f" Brand: {product.get('brand', 'N/A')}")
print(f" Price: {product.get('currency', '')} {product.get('price', 'N/A')}")
print(f" URL: {product.get('url')}") # This will now be in canonical format
# Display discount information if available
if 'original_price' in product:
print(f" Original Price: {product.get('currency', '')} {product.get('original_price')}")
print(f" Discount: {product.get('discount_percent', 'N/A')}% off")
# Display ratings and reviews
if 'rating' in product:
print(f" Rating: {product.get('rating')} / 5.0 ({product.get('reviews_count', 0)} reviews)")
# Display color variants if available
if 'color_variants' in product:
variant_count = len(product['color_variants'])
print(f" Available in {variant_count} colors:")
for v in product['color_variants'][:3]:
print(f" - {v['name']}: {v['url']}")
if variant_count > 3:
print(f" - ...and {variant_count-3} more colors")
# Display badge information if available
if 'badge' in product:
print(f" Badge: {product.get('badge')}")
# Display Prime eligibility
prime_status = "Yes" if product.get('prime', False) else "No"
print(f" Prime Eligible: {prime_status}")
# Display delivery information
if 'delivery_info' in product:
print(f" Delivery: {product.get('delivery_info')}")
# Display ASIN
print(f" ASIN: {product.get('asin', 'N/A')}")
# Show summary for remaining products
if len(products) > detailed_count:
print(f"\n... and {len(products) - detailed_count} more products")
# Save all results to JSON for reference
with open("enhanced_search_results.json", "w") as f:
json.dump(products, f, indent=2)
print(f"\nFull search results saved to enhanced_search_results.json")
else:
print("No products found or search failed.")
def example_search_by_url(url, max_pages: int = 5):
"""Example: Searching for products with enhanced parsing"""
print_section("Enhanced Product Search Example")
# Create a scraper with configuration
scraper = AmazonScraper(
country_code="in",
impersonate="chrome119"
)
# Method 2: Search using a pre-constructed URL
print(f"\n--- Method 2: Enhanced Search by URL ---")
print(f"Searching with URL: {url}")
# Search with 5 pages of results for demonstration
products = scraper.search_products(search_url=url, max_pages=max_pages)
# Display the enhanced results
if products:
print(f"\nFound {len(products)} products:")
# Show more detailed information for the first few products
detailed_count = min(3, len(products))
for i, product in enumerate(products[:detailed_count], 1):
print(f"\n{i}. {product.get('title', 'N/A')}")
print(f" Brand: {product.get('brand', 'N/A')}")
print(f" Price: {product.get('currency', '')} {product.get('price', 'N/A')}")
print(f" URL: {product.get('url')}") # This will now be in canonical format
# Display discount information if available
if 'original_price' in product:
print(f" Original Price: {product.get('currency', '')} {product.get('original_price')}")
print(f" Discount: {product.get('discount_percent', 'N/A')}% off")
# Display ratings and reviews
if 'rating' in product:
print(f" Rating: {product.get('rating')} / 5.0 ({product.get('reviews_count', 0)} reviews)")
# Display color variants if available
if 'color_variants' in product:
variant_count = len(product['color_variants'])
print(f" Available in {variant_count} colors:")
for v in product['color_variants'][:3]:
print(f" - {v['name']}: {v['url']}")
if variant_count > 3:
print(f" - ...and {variant_count-3} more colors")
# Display badge information if available
if 'badge' in product:
print(f" Badge: {product.get('badge')}")
# Display Prime eligibility
prime_status = "Yes" if product.get('prime', False) else "No"
print(f" Prime Eligible: {prime_status}")
# Display delivery information
if 'delivery_info' in product:
print(f" Delivery: {product.get('delivery_info')}")
# Display ASIN
print(f" ASIN: {product.get('asin', 'N/A')}")
# Show summary for remaining products
if len(products) > detailed_count:
print(f"\n... and {len(products) - detailed_count} more products")
# Save all results to JSON for reference
with open("enhanced_search_results_by_url.json", "w") as f:
json.dump(products, f, indent=2)
print(f"\nFull search results saved to enhanced_search_results_by_url.json")
else:
print("No products found or search failed.")
def example_config():
"""Example: Demonstrating configuration options"""
print_section("Configuration Example")
# Create a scraper
scraper = AmazonScraper()
print("Default configuration:")
print(scraper.user_config)
# Method 1: Configure using string format
print("\nUpdating with string configuration:")
scraper.config('MAX_RETRIES = 5, REQUEST_TIMEOUT = 30, DELAY_BETWEEN_REQUESTS = (3, 8)')
print(scraper.user_config)
# Method 2: Configure using keyword arguments
print("\nUpdating with keyword arguments:")
scraper.config(MAX_RETRIES=4, DEFAULT_IMPERSONATE="safari15")
print(scraper.user_config)
if __name__ == "__main__":
# Uncomment the examples you want to run
# example_config()
example_product_detail()
# example_search_by_query("men sneakers size 9") # Show the enhanced search capabilities
# example_search_by_url("https://www.amazon.in/s?k=shoes+for+men&rh=n%3A1571283031%2Cp_6%3AA1WYWER0W24N8S&dc", max_pages=200)