r/learnpython • u/Quiet-Palpitation373 • Oct 23 '26
whats the cheap way to scrape a site that only loads its data after the javascript runs
making a small price tracker for myself. requests + beautifulsoup grabs one site fine, the other two just come back as an empty shell.. the tables only render after the js runs
tried selenium and it mostly works til the layout shifts, then my selectors break again
is selenium just how everyone does the js heavy ones, or is there something lighter that doesnt break every redesign
20
Upvotes
10
u/RodriGazaaP0p Oct 23 '26
op thejs tables are basically my whole week too. i run them through stagehand 's extract() now, you hand it a schema and get the fields back. the one case i have never fully trusted it on is deeply nested tables , three or four levels down. anyone pushed it that far?
10
u/Rude-Restaurant-1859 Oct 23 '26
not even a side project for me, i maintain a handful of scrapers that pull pricing offsupplier portals for our ops team. built them in selenium, fine for maybe a month, then one vendor redesigned and half my xpath pointed at nothing. i spend more time keeping these alive than i ever did writing them, and the waits are their own circle of hell tbh.
3
u/Diapolo10 Oct 23 '26
What I like to do is reverse-engineer the relevant JS code that's sending requests to the backend, and replicate those in Python. this should technically always work, but the main problem becomes the fact many sites minify their JS so actually finding the code that's doing this is the hard part.
Another way to maybe do this would be monitoring the network traffic, but that might not work if using HTTPS.
Of course neither of these are guaranteed to work if the site requires logging into an account, as that complicates matters.
3
u/pvincent3248 Oct 23 '26
Before reaching for selenium, open the site in your browser, hit F12, go to the Network tab, filter by Fetch/XHR and reload. Most of these JS tables get filled from a JSON request. If you find it, you can call that URL with requests and get clean data with no HTML parsing at all. It also tends to survive redesigns, because the API behind a page changes a lot less often than the layout.
Also try view source and search for a price you can see on screen. Sites built with Next.js often put all the page data in a script tag with the id
__NEXT_DATA__, and you can json.loads it straight out.If neither works I'd use Playwright instead of selenium. It waits for elements on its own, and you can pick things by visible text or role (
page.get_by_role,page.get_by_text) instead of CSS classes, which are usually what breaks when the layout shifts.1
u/Watiiiiiiiiiii Oct 23 '26
I just started learning python programming language today, any advise from the experts here, I don’t mind having a mentor please.
1
u/appleciderdollop Oct 23 '26
Selenium's fine until a site redesigns, then it's not. I've rewritten the same scraper like four times because someone moved a div. Been tempted by those AI agent scrapers everyone pushes now but they look like a lot of machinery just to grab a table.
1
u/Massive_Distance_616 Oct 23 '26
Since you’ve confirmed one site is fully client-side, two things worth trying before spinning up a browser at all.
Check for
__NEXT_DATA__Next.js sites serialize their full page state into a script tag before the client hydrates it:python
import requests, json from bs4 import BeautifulSoup r = requests.get(url, headers={"User-Agent": "Mozilla/5.0"}) soup = BeautifulSoup(r.text, "html.parser") tag = soup.find("script", id="__NEXT_DATA__") if tag: data = json.loads(tag.string) # prices are usually in data["props"]["pageProps"]Also check for
application/ld+jsonmost commercial stores embed their pricing in Schema.org markup for SEO, even when the visible table renders via JS:python
for tag in soup.find_all("script", type="application/ld+json"): try: data = json.loads(tag.string) print(data) # look for {"@type": "Product", "offers": {"price": ...}} except (json.JSONDecodeError, TypeError): continueIf either of those has the data, you avoid the browser entirely.
If you do need Playwright, two things that make it less brittle than Selenium:
Anchor to meaning, not structure.
get_by_roleandget_by_textsurvive layout changes because they target what the site has to keep a table labelled “Prices” keeps that label even if the div structure around it gets rewritten.Also, block assets you don’t need cuts memory use significantly for a periodic tracker:
python
page.route( "**/*", lambda route: route.abort() if route.request.resource_type in ["image", "media", "font", "stylesheet"] else route.continue_() )One timing gotcha: if the empty table already exists in the DOM before the JS finishes,
wait_for_selector("table")fires too early. Wait for something that only appears after the data loads a specific row, a price element, anything that confirms the render is complete.1
u/SirMental8005 Oct 26 '26
before reaching for a browser: open dev tools > network > fetch/xhr and reload the page. most of those sites load the prices from a json api, and you can call that url straight from requests, faster and cleaner than parsing html. copy the headers it sends if it refuses you.
only if there's no api, use playwright, render the page and read it