Skip to content

Commit ba26ae3

Browse files
authored
Merge pull request #3 from MrManiesh/main
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.
2 parents 3e3f1ab + 28898e0 commit ba26ae3

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

amazon.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,23 @@
1515
class amazon:
1616
def __init__(self, url):
1717
self.url = url
18-
request_status_code = requests.get(url, headers=header).status_code
19-
if request_status_code != 200:
20-
sys.exit(f"Unable to get the page. Error code: {request_status_code}")
18+
response = requests.get(url, headers=header)
19+
if response.status_code != 200:
20+
sys.exit(f"Unable to get the page. Error code: {response.status_code}")
2121

22-
html_text = requests.get(url, headers=header).text
22+
html_text = response.content
2323

2424
soup = BeautifulSoup(html_text, 'lxml')
25-
26-
25+
2726
product_html_element = soup.find('span', id='productTitle')
27+
2828
if self.__check_if_product_exists(product_html_element):
2929
self.name = product_html_element.text.strip()
30-
30+
self.price = soup.find('span', class_="a-size-base a-color-price a-color-price").text
31+
3132
else:
3233
sys.exit("Unable to get the product. Please check the URL and try again.")
3334

34-
self.price = soup.find('span', class_='a-price-whole').text
35-
36-
3735
def __check_if_product_exists(self, soup):
3836
if soup is None:
3937
return False
@@ -52,11 +50,12 @@ def search_item(prod_name):
5250
prod_name = prod_name.replace(" ", "+")
5351
url = "https://www.amazon.in/s?k=" + prod_name
5452

55-
request_status_code = requests.get(url, headers=header).status_code
56-
if request_status_code != 200:
57-
sys.exit(f"Unable to get the page. Error code: {request_status_code}")
53+
response = requests.get(url, headers=header)
54+
55+
if response.status_code != 200:
56+
sys.exit(f"Unable to get the page. Error code: {response.status_code}")
5857

59-
html_text = requests.get(url, headers=header).text
58+
html_text = response.text
6059

6160
soup = BeautifulSoup(html_text, 'lxml')
6261

flipkart.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
class flipkart:
1515
def __init__(self, url):
1616
self.url = url
17-
request_status_code = requests.get(url, headers=header).status_code
18-
if request_status_code != 200:
19-
sys.exit(f"Unable to get the page. Error code: {request_status_code}")
20-
21-
html_text = requests.get(url, headers=header).text
17+
response = requests.get(url, headers=header)
2218

19+
if response.status_code != 200:
20+
sys.exit(f"Unable to get the page. Error code: {response.status_code}")
21+
22+
html_text = response.text
2323
soup = BeautifulSoup(html_text, 'lxml')
2424

2525
product_html_element = soup.find('span', class_='B_NuCI')
@@ -49,11 +49,12 @@ def search_item(prod_name):
4949
prod_name = prod_name.replace(" ", "+")
5050
url = "https://www.flipkart.com/search?q=" + prod_name
5151

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

56-
html_text = requests.get(url, headers=header).text
57+
html_text = response.text
5758

5859
soup = BeautifulSoup(html_text, 'lxml')
5960

0 commit comments

Comments
 (0)