Skip to content

Commit 813c6c3

Browse files
authored
Merge pull request #869 from nickdark/raspberry-pi-webscraper
Raspberry PI Locator
2 parents dd3ee47 + 40ff04d commit 813c6c3

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

raspberry_pi_locator/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# raspberry-pi-locator
2+
3+
A simple Python script to check popular Raspberry PI-certified shops for availability of Model 4s.
4+
5+
## Setup instructions
6+
7+
Simply install dependencies (ideally in a virtual environment):
8+
9+
```shell
10+
python -m venv venv
11+
source /venv/bin/activate
12+
pip install -r requirements.txt
13+
```
14+
15+
and then run the script:
16+
```shell
17+
python main.py
18+
```
19+
20+
## Output
21+
22+
The script returns formatted terminal output with URL links to the located Raspberry PIs if any were found.
23+
24+
## Author(s)
25+
26+
Nicholas Prieto

raspberry_pi_locator/main.py

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import requests
2+
from bs4 import BeautifulSoup
3+
4+
5+
def scrape_adafruit():
6+
url = "https://www.adafruit.com/product/4295"
7+
response = requests.get(url)
8+
9+
soup = BeautifulSoup(response.content, "html.parser")
10+
options = soup.find(class_="meta_pid_boxes").find_all("a")
11+
print("--- Adafruit ---")
12+
for option in options:
13+
status = option.find(class_="meta_pid_box_status").get_text().lower()
14+
ram_size = option.find(class_="stripes_oos_tag").get_text().strip()
15+
if "in" in status:
16+
print(ram_size, "is available -", url)
17+
print()
18+
19+
20+
def scrape_pishop_us():
21+
urls = {
22+
"1gb": "https://www.pishop.us/product/raspberry-pi-4-model-b-1gb/",
23+
"2gb": "https://www.pishop.us/product/raspberry-pi-4-model-b-2gb/",
24+
"4gb": "https://www.pishop.us/product/raspberry-pi-4-model-b-4gb/",
25+
"8gb": "https://www.pishop.us/product/raspberry-pi-4-model-b-8gb/",
26+
}
27+
print("--- PiShop US ---")
28+
for ram_size, url in urls.items():
29+
response = requests.get(url)
30+
soup = BeautifulSoup(response.content, "html.parser")
31+
status = soup.find(id="form-action-addToCart")["value"].lower()
32+
if "in" in status:
33+
print(ram_size, "is available -", url)
34+
print()
35+
36+
37+
def scrape_vilros():
38+
urls = {
39+
"1gb": "https://vilros.com/products/raspberry-pi-4-model-b",
40+
"2gb": "https://vilros.com/collections/raspberry-pi/products/raspberry-pi-4-2gb-ram",
41+
"4gb": "https://vilros.com/collections/raspberry-pi/products/raspberry-pi-4-4gb-ram",
42+
"8gb": "https://vilros.com/collections/raspberry-pi/products/raspberry-pi-4-model-b-8gb-ram",
43+
}
44+
print("--- Vilros ---")
45+
for ram_size, url in urls.items():
46+
response = requests.get(url)
47+
soup = BeautifulSoup(response.content, "html.parser")
48+
status = (
49+
soup.select("span[data-add-to-cart-text]")[0].get_text().strip().lower()
50+
)
51+
if "out" not in status:
52+
print(ram_size, "is available -", url)
53+
print()
54+
55+
56+
def scrape_chicago_electronics():
57+
urls = {
58+
"1gb": "https://chicagodist.com/products/raspberry-pi-4-model-b-1gb",
59+
"2gb": "https://chicagodist.com/products/raspberry-pi-4-model-b-2gb",
60+
"4gb": "https://chicagodist.com/products/raspberry-pi-4-model-b-4gb",
61+
"8gb": "https://chicagodist.com/products/raspberry-pi-4-model-b-8gb",
62+
}
63+
print("--- Chicago Electronic Distributors ---")
64+
for ram_size, url in urls.items():
65+
response = requests.get(url)
66+
soup = BeautifulSoup(response.content, "html.parser")
67+
status = soup.find(class_="modal_price").find("span").get_text().strip().lower()
68+
if "out" not in status:
69+
print(ram_size, "is available -", url)
70+
print()
71+
72+
73+
def scrape_okdo():
74+
urls = {
75+
"2gb": "https://www.okdo.com/us/p/raspberry-pi-4-model-b-2gb-2/"
76+
}
77+
print("--- OKdo ---")
78+
for ram_size, url in urls.items():
79+
response = requests.get(url)
80+
soup = BeautifulSoup(response.content, "html.parser")
81+
status = soup.find(class_="c-stock-level").get_text().strip().lower()
82+
if "out" not in status:
83+
print(ram_size, "is available -", url)
84+
print()
85+
86+
87+
def scrape_canakit():
88+
urls = {
89+
"1gb": "https://www.canakit.com/raspberry-pi-4.html",
90+
"2gb": "https://www.canakit.com/raspberry-pi-4-2gb.html",
91+
"4gb": "https://www.canakit.com/raspberry-pi-4-4gb.html",
92+
"8gb": "https://www.canakit.com/raspberry-pi-4-8gb.html",
93+
}
94+
print("--- Canakit ---")
95+
for ram_size, url in urls.items():
96+
response = requests.get(url)
97+
soup = BeautifulSoup(response.content, "html.parser")
98+
status = (
99+
soup.find(id="ProductAddToCartDiv").find("span").get_text().strip().lower()
100+
)
101+
if "out" not in status:
102+
print(ram_size, "is available -", url)
103+
print()
104+
105+
106+
def main():
107+
scrape_adafruit()
108+
scrape_pishop_us()
109+
scrape_vilros()
110+
scrape_chicago_electronics()
111+
scrape_okdo()
112+
scrape_canakit()
113+
114+
115+
if __name__ == "__main__":
116+
main()

raspberry_pi_locator/requirements.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
beautifulsoup4==4.11.1
2+
black==22.8.0
3+
bs4==0.0.1
4+
certifi==2022.9.24
5+
charset-normalizer==2.1.1
6+
click==8.1.3
7+
idna==3.4
8+
mypy-extensions==0.4.3
9+
pathspec==0.10.1
10+
platformdirs==2.5.2
11+
requests==2.28.1
12+
soupsieve==2.3.2.post1
13+
tomli==2.0.1
14+
urllib3==1.26.12

0 commit comments

Comments
 (0)