5
5
6
6
"""
7
7
8
- from urllib .error import HTTPError
9
-
8
+ import httpx
10
9
from bs4 import BeautifulSoup
11
- from requests import exceptions , get
12
10
13
- BASE_URL = "https://www.wellrx.com/prescriptions/{0 }/{1 }/?freshSearch=true"
11
+ BASE_URL = "https://www.wellrx.com/prescriptions/{}/{}/?freshSearch=true"
14
12
15
13
16
14
def fetch_pharmacy_and_price_list (drug_name : str , zip_code : str ) -> list | None :
17
15
"""[summary]
18
16
19
17
This function will take input of drug name and zipcode,
20
18
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.
23
21
24
22
Args:
25
23
drug_name (str): [Drug name]
@@ -28,12 +26,12 @@ def fetch_pharmacy_and_price_list(drug_name: str, zip_code: str) -> list | None:
28
26
Returns:
29
27
list: [List of pharmacy name and price]
30
28
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
37
35
"""
38
36
39
37
try :
@@ -42,25 +40,22 @@ def fetch_pharmacy_and_price_list(drug_name: str, zip_code: str) -> list | None:
42
40
return None
43
41
44
42
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 ()
49
44
50
45
# Scrape the data using bs4
51
46
soup = BeautifulSoup (response .text , "html.parser" )
52
47
53
48
# This list will store the name and price.
54
49
pharmacy_price_list = []
55
50
56
- # Fetch all the grids that contains the items.
51
+ # Fetch all the grids that contain the items.
57
52
grid_list = soup .find_all ("div" , {"class" : "grid-x pharmCard" })
58
53
if grid_list and len (grid_list ) > 0 :
59
54
for grid in grid_list :
60
55
# Get the pharmacy price.
61
56
pharmacy_name = grid .find ("p" , {"class" : "list-title" }).text
62
57
63
- # Get price of the drug.
58
+ # Get the price of the drug.
64
59
price = grid .find ("span" , {"p" , "price price-large" }).text
65
60
66
61
pharmacy_price_list .append (
@@ -72,7 +67,7 @@ def fetch_pharmacy_and_price_list(drug_name: str, zip_code: str) -> list | None:
72
67
73
68
return pharmacy_price_list
74
69
75
- except (HTTPError , exceptions . RequestException , ValueError ):
70
+ except (httpx . HTTPError , ValueError ):
76
71
return None
77
72
78
73
0 commit comments