Create scraping_utils.py
Browse files- scraping_utils.py +38 -0
scraping_utils.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
|
| 4 |
+
def scrape_farnell(component_name):
|
| 5 |
+
url = f"https://uk.farnell.com/search?st={component_name}"
|
| 6 |
+
response = requests.get(url)
|
| 7 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 8 |
+
|
| 9 |
+
# Extract component data (example)
|
| 10 |
+
components = []
|
| 11 |
+
for item in soup.find_all("div", class_="product"):
|
| 12 |
+
name = item.find("h3").text.strip()
|
| 13 |
+
description = item.find("p", class_="description").text.strip()
|
| 14 |
+
datasheet_link = item.find("a", text="Datasheet")["href"]
|
| 15 |
+
components.append({
|
| 16 |
+
"name": name,
|
| 17 |
+
"description": description,
|
| 18 |
+
"datasheet_link": datasheet_link
|
| 19 |
+
})
|
| 20 |
+
return components
|
| 21 |
+
|
| 22 |
+
def scrape_digikey(component_name):
|
| 23 |
+
url = f"https://www.digikey.com/en/products/result?keywords={component_name}"
|
| 24 |
+
response = requests.get(url)
|
| 25 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 26 |
+
|
| 27 |
+
# Extract component data (example)
|
| 28 |
+
components = []
|
| 29 |
+
for item in soup.find_all("tr", class_="product-row"):
|
| 30 |
+
name = item.find("td", class_="digikey-part-number").text.strip()
|
| 31 |
+
description = item.find("td", class_="description").text.strip()
|
| 32 |
+
datasheet_link = item.find("a", text="Datasheet")["href"]
|
| 33 |
+
components.append({
|
| 34 |
+
"name": name,
|
| 35 |
+
"description": description,
|
| 36 |
+
"datasheet_link": datasheet_link
|
| 37 |
+
})
|
| 38 |
+
return components
|