Skip to content

Commit

Permalink
Merge pull request #3 from MrManiesh/main
Browse files Browse the repository at this point in the history
Changes made:
+ In `amazon.py`: You have moved the price information inside the `if` block (line 30).
+ In both `amazon.py` and `flipkart.py`: You have optimized the code by calling the `requests.get` method only once instead of twice, which reduces the execution time and the network traffic.
  • Loading branch information
yasharya2901 authored Dec 2, 2023
2 parents 3e3f1ab + 28898e0 commit ba26ae3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
27 changes: 13 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,12 @@ 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)

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
19 changes: 10 additions & 9 deletions flipkart.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
class flipkart:
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}")

html_text = requests.get(url, headers=header).text
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 = response.text
soup = BeautifulSoup(html_text, 'lxml')

product_html_element = soup.find('span', class_='B_NuCI')
Expand Down Expand Up @@ -49,11 +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
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.text

soup = BeautifulSoup(html_text, 'lxml')

Expand Down

0 comments on commit ba26ae3

Please sign in to comment.