Created
June 21, 2020 06:50
-
-
Save spjoshis/47587ec13f9eab12fb63c4a2606dfbed to your computer and use it in GitHub Desktop.
Scrape script to get latest mobile phones from Amazon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Scrape latest mobile phones from Amazon """ | |
# virtualenv -p /usr/bin/python3 100DaysofCode | |
import requests | |
from lxml import html | |
from bs4 import BeautifulSoup | |
import json | |
session_requests = requests.session() | |
# Get login csrf token | |
url = 'https://www.amazon.in/s?k=mobile&s=date-desc-rank&qid=1592716488&ref=sr_st_date-desc-rank' | |
result = session_requests.get(url, verify=True) | |
tree = html.fromstring(result.text) | |
# Fetch product Details | |
products = list(set(tree.xpath("//a[@class='a-link-normal a-text-normal']"))) | |
product_name = [j.text for j in [i.xpath('span')[0] for i in products]] | |
product_link = [i.attrib['href'] for i in products] | |
product_price_tags = list(set(tree.xpath("//span[@class='a-price-whole']"))) | |
product_price = [i.text for i in product_price_tags] | |
products = [] | |
i=0 | |
for name in product_name: | |
products.append({ | |
'name': name, | |
'price': product_price[i], | |
'link': product_link[i] | |
}) | |
i=+1 | |
print(json.dumps(products, indent=4)) | |
""" Output """ | |
``` | |
[ | |
{ | |
"price": "7,999", | |
"link": "/Test-Exclusive-646/dp/B07HGJKDRR?dchild=1", | |
"name": "Samsung Galaxy M40 (Midnight Blue, 6GB RAM, 128GB Storage)" | |
}, | |
{ | |
"price": "16,999", | |
"link": "/Samsung-Galaxy-Charcoal-Black-32GB/dp/B07HGKHV8Y?dchild=1", | |
"name": "Samsung Galaxy M10 (Charcoal Black, 3GB RAM, 32GB Storage)" | |
}, | |
{ | |
"price": "16,999", | |
"link": "/Samsung-Galaxy-Charcoal-Black-32GB/dp/B07HGKHV8Y?dchild=1", | |
"name": "Samsung Galaxy M31 (Ocean Blue, 6GB RAM, 64GB Storage)" | |
}, | |
{ | |
"price": "16,999", | |
"link": "/Samsung-Galaxy-Charcoal-Black-32GB/dp/B07HGKHV8Y?dchild=1", | |
"name": "Redmi Note 8 Pro (Electric Blue, 6GB RAM, 128GB Storage with Helio G90T Processor)" | |
}, | |
{ | |
"price": "16,999", | |
"link": "/Samsung-Galaxy-Charcoal-Black-32GB/dp/B07HGKHV8Y?dchild=1", | |
"name": "Samsung Galaxy M31 (Ocean Blue, 6GB RAM, 128GB Storage)" | |
}, | |
{ | |
"price": "16,999", | |
"link": "/Samsung-Galaxy-Charcoal-Black-32GB/dp/B07HGKHV8Y?dchild=1", | |
"name": "Redmi Note 8 (Neptune Blue, 4GB RAM, 64GB Storage)" | |
}, | |
{ | |
"price": "16,999", | |
"link": "/Samsung-Galaxy-Charcoal-Black-32GB/dp/B07HGKHV8Y?dchild=1", | |
"name": "Samsung Galaxy M30s (Opal Black, 4GB RAM, 64GB Storage)" | |
}, | |
{ | |
"price": "16,999", | |
"link": "/Samsung-Galaxy-Charcoal-Black-32GB/dp/B07HGKHV8Y?dchild=1", | |
"name": "Panasonic Eluga Ray 810 (4GB RAM, 64GB Storage) (Starry Black)" | |
}, | |
{ | |
"price": "16,999", | |
"link": "/Samsung-Galaxy-Charcoal-Black-32GB/dp/B07HGKHV8Y?dchild=1", | |
"name": "Redmi Note 8 Pro (Electric Blue, 6GB RAM, 64GB Storage with Helio G90T Processor)" | |
}, | |
{ | |
"price": "16,999", | |
"link": "/Samsung-Galaxy-Charcoal-Black-32GB/dp/B07HGKHV8Y?dchild=1", | |
"name": "Vivo Y15 (Aqua Blue, 4GB RAM, 64GB Storage) with No Cost EMI/Additional Exchange" | |
} | |
] | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment