r/WebScrapingInsider • • 1d ago

While web crawling with Python my requests + BeautifulSoup crawler keeps looping and leaving the site, should I fix it myself or switch to Scrapy?

So i built a basic crawler with requests and BeautifulSoup. It starts on one page, grabs all the links and visits them. It worked for a bit but now it keeps visiting the same pages again and again, and once it wandered off to a totally different website (some social media link i think) and i had to ctrl+c it.

People keep telling me to use Scrapy instead but idk if thats overkill for me, im still on the basics (functions, dicts, a little async😉). Does Scrapy handle the visited pages / staying on one site stuff for you, or do i still have to figure that out myself? Or should i fix my own loop first so i actually understand it?

Also no idea how fast is ok to run it, i dont want to annoy the site.

3 Upvotes

4 comments sorted by

3

u/simarnoor 1d ago

Quick definition.. a crawler is a queue + seen-set loop. Pop a URL, fetch it, pull out the links, push the ones you haven’t seen, repeat. Scraping is what you extract from each page along the way. You built the first half, minus the guard rails.

Above two problems are the classic ones.

No seen = revisits. No boundary = it follows any link anywhere. Unbounded work in, unbounded work out. Predictable. so yeah, pretty normal rn.

What i will add before Scrapy

  • seen-set/queue (strip the #fragment, or /a and /a#top count as two pages)
  • same-host check
  • hard page cap
  • sleep between requests
  • one log line per fetch (status/seconds/url), so you can tell what it’s doing

```

host = urlparse(start).netloc
queue, seen = deque([start]), {start}
while queue:
url = queue.popleft()
html = requests.get(url, timeout=10).text
for a in BeautifulSoup(html, "html.parser").find_all("a", href=True):
link, _ = urldefrag(urljoin(url, a["href"]))
if urlparse(link).netloc == host and link not in seen:
seen.add(link)
queue.append(link)
time.sleep(1)
```

Scrapy does the same things, configured instead of coded. Duplicates are filtered by default. Off-site filtering needs allowed_domains set; empty means every request is allowed. Speed is where people get surprised. A project from scrapy startproject ships with a 1s DOWNLOAD_DELAY, one request at a time per domain, and robots.txt obeyed. Outside that template the defaults are 0 delay, robots off.

Fix the loop first. Switch when you want retries/throttling/exports without writing them. robots.txt isn’t access control, but well-behaved crawlers follow it, so check it either way.

Edit:
Post the log output here or DM me if it still misbehaves..

1

u/Shinepie-Louchene 1d ago

for the speed part, sticking to 1 request every couple of seconds is usually safe enough to not trigger anything or annoy the host. you can also check their robots.txt file to see if they specify a crawl delay.

1

u/Artistic_Map2243 1d ago

Is this for collecting data or are you just walking the links to see the structure? changes the approach a little.

Either way, staying on one site is genuinely just a domain check. urlparse the link, compare .netloc to your start host, skip it if it doesnt match. thats the whole 'leaving the site' fix.. one if statement.

The revisits are the set thing people already mentioned. Nah you dont need Scrapy for any of this yet. we reach for frameworks way too early when a 10 line guard would do.

1

u/Habitualcaveman 20h ago

Scrapy all the way, took a minute learn but I’ve never looked back.Â