55
66"""
77
8- from urllib .error import HTTPError
9-
8+ import httpx
109from bs4 import BeautifulSoup
11- from requests import exceptions , get
1210
13- BASE_URL = "https://www.wellrx.com/prescriptions/{0 }/{1 }/?freshSearch=true"
11+ BASE_URL = "https://www.wellrx.com/prescriptions/{}/{}/?freshSearch=true"
1412
1513
1614def fetch_pharmacy_and_price_list (drug_name : str , zip_code : str ) -> list | None :
1715 """[summary]
1816
1917 This function will take input of drug name and zipcode,
2018 then request to the BASE_URL site.
21- Get the page data and scrape it to the generate the
22- list of lowest prices for the prescription drug.
19+ Get the page data and scrape it to generate the
20+ list of the lowest prices for the prescription drug.
2321
2422 Args:
2523 drug_name (str): [Drug name]
@@ -28,12 +26,12 @@ def fetch_pharmacy_and_price_list(drug_name: str, zip_code: str) -> list | None:
2826 Returns:
2927 list: [List of pharmacy name and price]
3028
31- >>> fetch_pharmacy_and_price_list(None, None)
32-
33- >>> fetch_pharmacy_and_price_list(None, 30303)
34-
35- >>> fetch_pharmacy_and_price_list("eliquis", None)
36-
29+ >>> print( fetch_pharmacy_and_price_list(None, None) )
30+ None
31+ >>> print( fetch_pharmacy_and_price_list(None, 30303) )
32+ None
33+ >>> print( fetch_pharmacy_and_price_list("eliquis", None) )
34+ None
3735 """
3836
3937 try :
@@ -42,25 +40,22 @@ def fetch_pharmacy_and_price_list(drug_name: str, zip_code: str) -> list | None:
4240 return None
4341
4442 request_url = BASE_URL .format (drug_name , zip_code )
45- response = get (request_url , timeout = 10 )
46-
47- # Is the response ok?
48- response .raise_for_status ()
43+ response = httpx .get (request_url , timeout = 10 ).raise_for_status ()
4944
5045 # Scrape the data using bs4
5146 soup = BeautifulSoup (response .text , "html.parser" )
5247
5348 # This list will store the name and price.
5449 pharmacy_price_list = []
5550
56- # Fetch all the grids that contains the items.
51+ # Fetch all the grids that contain the items.
5752 grid_list = soup .find_all ("div" , {"class" : "grid-x pharmCard" })
5853 if grid_list and len (grid_list ) > 0 :
5954 for grid in grid_list :
6055 # Get the pharmacy price.
6156 pharmacy_name = grid .find ("p" , {"class" : "list-title" }).text
6257
63- # Get price of the drug.
58+ # Get the price of the drug.
6459 price = grid .find ("span" , {"p" , "price price-large" }).text
6560
6661 pharmacy_price_list .append (
@@ -72,7 +67,7 @@ def fetch_pharmacy_and_price_list(drug_name: str, zip_code: str) -> list | None:
7267
7368 return pharmacy_price_list
7469
75- except (HTTPError , exceptions . RequestException , ValueError ):
70+ except (httpx . HTTPError , ValueError ):
7671 return None
7772
7873
0 commit comments