Skip to content

Commit dffe61e

Browse files
committed
Added ASIN to amazon2csv.py and updated tests and README.md
1 parent e8d0020 commit dffe61e

3 files changed

Lines changed: 23 additions & 17 deletions

File tree

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ amazon2csv.py --keywords="Python programming" --maxproductnb=2
3535
```
3636

3737
```csv
38-
Product title,Rating,Number of customer reviews,Product URL
39-
"Python Crash Course: A Hands-On, Project-Based Introduction to Programming",4.5,309,https://www.amazon.com/Python-Crash-Course-Hands-Project-Based/dp/1593276036
40-
"A Smarter Way to Learn Python: Learn it faster. Remember it longer.",4.8,144,https://www.amazon.com/Smarter-Way-Learn-Python-Remember-ebook/dp/B077Z55G3B
38+
Product title,Rating,Number of customer reviews,Product URL,ASIN
39+
"Python Crash Course: A Hands-On, Project-Based Introduction to Programming",4.5,357,https://www.amazon.com/Python-Crash-Course-Hands-Project-Based/dp/1593276036,1593276036
40+
"A Smarter Way to Learn Python: Learn it faster. Remember it longer.",4.7,358,https://www.amazon.com/Smarter-Way-Learn-Python-Remember-ebook/dp/B077Z55G3B,B077Z55G3B
4141
```
4242

4343
You can also pass a search url (if you added complex filters for example), and save it to a file :
@@ -65,7 +65,8 @@ import amazonscraper
6565
results = amazonscraper.search("Python programming")
6666

6767
for result in results:
68-
print("%s (%s out of 5 stars, %s customer reviews) : %s" % (result.title, result.rating, result.review_nb, result.url))
68+
print("{} [ASIN = {}] ({} out of 5 stars, {} customer reviews) : {}".format(
69+
result.title, result.asin, result.rating, result.review_nb, result.url))
6970

7071
print("Number of results : %d" % (len(results)))
7172

@@ -74,10 +75,11 @@ print("Number of results : %d" % (len(results)))
7475
Which will output :
7576

7677
```
77-
Learning Python, 5th Edition (4.0 out of 5 stars, 293 customer reviews) : https://www.amazon.com/Learning-Python-5th-Mark-Lutz/dp/1449355730
78-
Fluent Python: Clear, Concise, and Effective Programming (4.6 out of 5 stars, 87 customer reviews) : https://www.amazon.com/Fluent-Python-Concise-Effective-Programming/dp/1491946008
78+
Python Crash Course: A Hands-On, Project-Based Introduction to Programming [ASIN = 1593276036] (4.5 out of 5 stars, 357 customer reviews) : https://www.amazon.com/Python-Crash-Course-Hands-Project-Based/dp/1593276036
79+
A Smarter Way to Learn Python: Learn it faster. Remember it longer. [ASIN = B077Z55G3B] (4.7 out of 5 stars, 358 customer reviews) : https://www.amazon.com/Smarter-Way-Learn-Python-Remember-ebook/dp/B077Z55G3B
80+
Learning Python, 5th Edition [ASIN = 1449355730] (4 out of 5 stars, 315 customer reviews) : https://www.amazon.com/Learning-Python-5th-Mark-Lutz/dp/1449355730
7981
[...]
80-
Number of results : 3000
82+
Number of results : 100
8183
```
8284

8385
### Attributes of the `Product` object

amazonscraper/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,25 @@ def __getitem__(self, key):
4545
def csv(self, separator=","):
4646
""" Returns a CSV string with the product info
4747
>>> p = Products([{'title':'Book title', 'rating': '4.2',\
48-
'review_nb': '15', 'url':'http://www.amazon.com/book'}])
48+
'review_nb': '15', 'url':'http://www.amazon.com/book', 'asin':'A12345'}])
4949
>>> p.csv()
5050
'Product title,Rating,Number of customer reviews,\
51-
Product URL\\n"Book title",4.2,15,http://www.amazon.com/book'
51+
Product URL,ASIN\\n"Book title",4.2,15,http://www.amazon.com/book,A12345'
5252
5353
>>> print(p.csv(separator=";"))
54-
Product title;Rating;Number of customer reviews;Product URL
55-
"Book title";4,2;15;http://www.amazon.com/book
54+
Product title;Rating;Number of customer reviews;Product URL;ASIN
55+
"Book title";4,2;15;http://www.amazon.com/book;A12345
5656
5757
>>> p2 = Products()
5858
>>> p2.csv()
59-
'Product title,Rating,Number of customer reviews,Product URL'
59+
'Product title,Rating,Number of customer reviews,Product URL,ASIN'
6060
"""
6161
csv_string = separator.join([
6262
"Product title",
6363
"Rating",
6464
"Number of customer reviews",
65-
"Product URL"])
65+
"Product URL",
66+
"ASIN"])
6667
for product in self:
6768
rating = product.rating
6869
if separator == ";": # French convention
@@ -72,7 +73,8 @@ def csv(self, separator=","):
7273
'"'+product.title+'"',
7374
rating,
7475
product.review_nb,
75-
product.url]))
76+
product.url,
77+
product.asin]))
7678
return csv_string
7779

7880

test/test_amazonscraper.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def test_amazonscraper_get_products_with_keywords():
1313

1414

1515
def test_amazonscraper_get_products_with_url():
16-
url = "https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=python"
16+
url = "https://www.amazon.com/s/\
17+
ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=python"
1718
products = amazonscraper.search(
1819
search_url=url,
1920
max_product_nb=_MAX_PRODUCT_NB)
@@ -26,6 +27,7 @@ def test_amazonscraper_get_products_with_url():
2627
assert product.review_nb != ""
2728
assert product.rating != ""
2829
assert product.url != ""
30+
assert product.asin != ""
2931

3032

3133
def test_amazonscraper_get_100_products():
@@ -42,5 +44,5 @@ def test_amazonscraper_csv_header():
4244
products = amazonscraper.search(
4345
keywords="Python",
4446
max_product_nb=1)
45-
assert "Product title,Rating,Number of customer reviews,Product URL\n" \
46-
in str(products.csv())
47+
assert "Product title,Rating,Number of customer reviews,Product URL,\
48+
ASIN\n" in str(products.csv())

0 commit comments

Comments
 (0)