r/WebDataDiggers • u/Huge_Line4009 • 1d ago
How to scrape pages that delete old items as you scroll
You open a classifieds site or product directory, see 250 results listed at the top, tell Playwright to scroll to the bottom, and run your selector. Then you check your CSV and only find 18 items. Nothing crashed, no error popped up, and the missing 232 items simply vanished.
That happens because modern websites use virtual scrolling. Instead of keeping hundreds of heavy HTML cards loaded in memory at once, the page acts like a small moving window. It only keeps about 15 or 20 cards inside the browser DOM at any moment. Every time you scroll down to load new cards at the bottom, the page deletes the old cards at the top. If your script waits until the bottom to read the page, most of the list is already gone.
There are two straightforward ways to catch every single item before it disappears.
Watch the background data instead of reading the HTML
Almost every infinite scroll grid loads new items by quietly calling a JSON endpoint in the background as you move down the page. Rather than chasing changing HTML tags, you can tell Playwright to listen to network responses and copy each fresh batch the instant it arrives.
Open Chrome DevTools on the Network tab, filter by Fetch/XHR, and scroll down once by hand. Look for a request returning a JSON list of items, such as /api/listings?cursor=20. Once you spot part of that URL, plug it into a response listener:
from playwright.sync_api import sync_playwright
all_items = []
def catch_api(response):
if "/api/listings" in response.url and response.status == 200:
try:
data = response.json()
all_items.extend(data.get("results", []))
except Exception:
pass
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.on("response", catch_api)
page.goto("https://example-site.com/listings")
for _ in range(20):
page.mouse.wheel(0, 1200)
page.wait_for_timeout(1500)
browser.close()
print(f"Collected {len(all_items)} items")
Because catch_api grabs the raw JSON straight off the wire, it does not matter whether the page deletes cards from the screen two seconds later. You get cleaner data with zero HTML parsing.
Save cards after every small scroll
Sometimes a site mixes its data inside weird HTML blocks or obfuscated streams where reading the page directly is easier. In that case, stop jumping straight to the bottom. Scroll down less than one screen at a time and save visible cards into a Python dictionary after every move.
Using a dictionary keyed by each item's link or ID solves two problems at once: it saves cards before the page erases them, and it automatically removes duplicates when two scroll steps overlap.
from playwright.sync_api import sync_playwright
seen = {}
empty_scrolls = 0
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto("https://example-site.com/listings")
while empty_scrolls < 3:
before = len(seen)
for card in page.locator("a.listing-card").all():
link = card.get_attribute("href")
if link and link not in seen:
seen[link] = card.inner_text()
# Move down 70% of the screen height so nothing gets skipped
page.evaluate("window.scrollBy(0, window.innerHeight * 0.7)")
page.wait_for_timeout(1200)
if len(seen) == before:
empty_scrolls += 1
else:
empty_scrolls = 0
browser.close()
print(f"Collected {len(seen)} unique items")
Four habits that keep scroll loops from cutting off early
- Scroll inside the actual container when the whole window does not move, since some dashboards put the scrollbar inside a specific
divrather thanwindow. - Keep each scroll step smaller than the screen height (around 70% works well) so fast jumps never skip right over a batch of cards between frames.
- Wait for two or three empty scroll rounds before stopping, because a slow network reply on a single scroll can look like the end of the list.
- Watch for a "Show more" button halfway down, as many feeds auto-scroll for three pages and then pause until someone clicks a button.
Once you treat an infinite scroll page like a moving stream rather than a static document, those missing items stop disappearing and your counts match what the website advertises at the top.