-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo1-content-fixed.py
More file actions
32 lines (27 loc) · 1010 Bytes
/
demo1-content-fixed.py
File metadata and controls
32 lines (27 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import requests
from bs4 import BeautifulSoup
BROWSERLESS_TOKEN = "YOUR_API_TOKEN_HERE"
# Fetch the page
response = requests.post(
f"https://production-sfo.browserless.io/content?token={BROWSERLESS_TOKEN}",
json={
"url": "https://www.scrapingcourse.com/javascript-rendering",
"waitForSelector": {
"selector": ".product-name",
"timeout": 10000
}
},
headers={"Content-Type": "application/json"}
)
# Parse the HTML
soup = BeautifulSoup(response.text, "html.parser")
# Extract product data
products = soup.select(".product-item")
print(f"Products found: {len(products)}\n")
for i, product in enumerate(products):
name = product.select_one(".product-name")
price = product.select_one(".product-price")
image = product.select_one("img")
print(f" {i + 1}. {name.text.strip() if name else '(empty)'} {price.text.strip() if price else '(empty)'}")
print(f" {image.get('src', '(empty)') if image else '(empty)'}")
print()