Skip to content

Commit

Permalink
Include price information for Amazon(which is missing in previous cod…
Browse files Browse the repository at this point in the history
…e) and optimize code for efficiency(made some chnages in basic code)
  • Loading branch information
MrManiesh committed Dec 2, 2023
1 parent 6576791 commit 630c82b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
30 changes: 16 additions & 14 deletions amazon.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,23 @@
class amazon:
def __init__(self, url):
self.url = url
request_status_code = requests.get(url, headers=header).status_code
if request_status_code != 200:
sys.exit(f"Unable to get the page. Error code: {request_status_code}")
response = requests.get(url, headers=header)
if response.status_code != 200:
sys.exit(f"Unable to get the page. Error code: {response.status_code}")

html_text = requests.get(url, headers=header).text
html_text = response.content

soup = BeautifulSoup(html_text, 'lxml')



product_html_element = soup.find('span', id='productTitle')

if self.__check_if_product_exists(product_html_element):
self.name = product_html_element.text.strip()

self.price = soup.find('span', class_="a-size-base a-color-price a-color-price").text

else:
sys.exit("Unable to get the product. Please check the URL and try again.")

self.price = soup.find('span', class_='a-price-whole').text


def __check_if_product_exists(self, soup):
if soup is None:
return False
Expand All @@ -52,11 +50,15 @@ def search_item(prod_name):
prod_name = prod_name.replace(" ", "+")
url = "https://www.amazon.in/s?k=" + prod_name

request_status_code = requests.get(url, headers=header).status_code
if request_status_code != 200:
sys.exit(f"Unable to get the page. Error code: {request_status_code}")
response = requests.get(url, headers=header)

for one in range(50):
if response.status_code != 200:
sys.exit(f"Unable to get the page. Error code: {response.status_code}")
else:
break

html_text = requests.get(url, headers=header).text
html_text = response.text

soup = BeautifulSoup(html_text, 'lxml')

Expand Down
8 changes: 4 additions & 4 deletions flipkart.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ def search_item(prod_name):
prod_name = prod_name.replace(" ", "+")
url = "https://www.flipkart.com/search?q=" + prod_name

request_status_code = requests.get(url, headers=header).status_code
response = requests.get(url, headers=header)

if request_status_code != 200:
sys.exit(f"- Unable to get the page. Error code: {request_status_code}")
if response.status_code != 200:
sys.exit(f"- Unable to get the page. Error code: {response.status_code}")

html_text = requests.get(url, headers=header).text
html_text = response.text

soup = BeautifulSoup(html_text, 'lxml')

Expand Down

0 comments on commit 630c82b

Please sign in to comment.