Skip to content

Commit b2a77cc

Browse files
Scraping prescription drug prices from Rx site using the prescription drug name and zipcode (TheAlgorithms#5967)
* add wellrx scraping * write test fix docs * fix resolve issues * black format. fix returns * type check fix for union * black formatted * Change requests after code review * add precommit changes * flake errors
1 parent 24d3cf8 commit b2a77cc

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""
2+
3+
Scrape the price and pharmacy name for a prescription drug from rx site
4+
after providing the drug name and zipcode.
5+
6+
"""
7+
8+
from typing import Union
9+
from urllib.error import HTTPError
10+
11+
from bs4 import BeautifulSoup
12+
from requests import exceptions, get
13+
14+
BASE_URL = "https://www.wellrx.com/prescriptions/{0}/{1}/?freshSearch=true"
15+
16+
17+
def fetch_pharmacy_and_price_list(drug_name: str, zip_code: str) -> Union[list, None]:
18+
"""[summary]
19+
20+
This function will take input of drug name and zipcode,
21+
then request to the BASE_URL site.
22+
Get the page data and scrape it to the generate the
23+
list of lowest prices for the prescription drug.
24+
25+
Args:
26+
drug_name (str): [Drug name]
27+
zip_code(str): [Zip code]
28+
29+
Returns:
30+
list: [List of pharmacy name and price]
31+
32+
>>> fetch_pharmacy_and_price_list(None, None)
33+
34+
>>> fetch_pharmacy_and_price_list(None, 30303)
35+
36+
>>> fetch_pharmacy_and_price_list("eliquis", None)
37+
38+
"""
39+
40+
try:
41+
42+
# Has user provided both inputs?
43+
if not drug_name or not zip_code:
44+
return None
45+
46+
request_url = BASE_URL.format(drug_name, zip_code)
47+
response = get(request_url)
48+
49+
# Is the response ok?
50+
response.raise_for_status()
51+
52+
# Scrape the data using bs4
53+
soup = BeautifulSoup(response.text, "html.parser")
54+
55+
# This list will store the name and price.
56+
pharmacy_price_list = []
57+
58+
# Fetch all the grids that contains the items.
59+
grid_list = soup.find_all("div", {"class": "grid-x pharmCard"})
60+
if grid_list and len(grid_list) > 0:
61+
for grid in grid_list:
62+
63+
# Get the pharmacy price.
64+
pharmacy_name = grid.find("p", {"class": "list-title"}).text
65+
66+
# Get price of the drug.
67+
price = grid.find("span", {"p", "price price-large"}).text
68+
69+
pharmacy_price_list.append(
70+
{
71+
"pharmacy_name": pharmacy_name,
72+
"price": price,
73+
}
74+
)
75+
76+
return pharmacy_price_list
77+
78+
except (HTTPError, exceptions.RequestException, ValueError):
79+
return None
80+
81+
82+
if __name__ == "__main__":
83+
84+
# Enter a drug name and a zip code
85+
drug_name = input("Enter drug name: ").strip()
86+
zip_code = input("Enter zip code: ").strip()
87+
88+
pharmacy_price_list: Union[list, None] = fetch_pharmacy_and_price_list(
89+
drug_name, zip_code
90+
)
91+
92+
if pharmacy_price_list:
93+
94+
print(f"\nSearch results for {drug_name} at location {zip_code}:")
95+
for pharmacy_price in pharmacy_price_list:
96+
97+
name = pharmacy_price["pharmacy_name"]
98+
price = pharmacy_price["price"]
99+
100+
print(f"Pharmacy: {name} Price: {price}")
101+
else:
102+
print(f"No results found for {drug_name}")

0 commit comments

Comments
 (0)