My Telegram scraper gave 6 results, then 2, then 6, then 2. Same input every time. This is how I found the reason.
The setup
The scraper reads public channels through the web preview at t.me/s/<channel>. One of its jobs is to find related channels. A big signal for that is forwards: if channel A forwards posts from channel B, they are probably in the same niche.
The symptom
I ran it twice with the same seed channel, 10 minutes apart. First run: 6 channels. Second run: 2 channels. The log said the second run found one reference less in the seed channel. That one missing reference removed 4 results, because the next step builds on it.
The wrong guess
My first idea was that Telegram sometimes does not serve the preview page. I added a retry for that. It changed nothing. The pages were fine.
The real cause
I logged one specific post in both runs. Same post ID, same text. In the good run the forward header had a link to the source channel. In the bad run it looked like this:
<div class="tgme_widget_message_forwarded_from accent_color">
Forwarded from
<span class="tgme_widget_message_forwarded_from_name"><span dir="auto"></span></span>
</div>
The header is there, but the name and the link are empty.
How it behaves
- It is not random per request. Four requests within four seconds all came back empty.
- Ten to thirty seconds later the same page had the source filled in.
- My guess is that the name is resolved lazily when the page has not been requested for a while. I cannot prove this.
- Some forwards never get a source at all, for example when the source is hidden. So "retry until it works" is not an option.
The fix
for attempt in range(3):
broken = {p["postId"] for p in page["posts"]
if p["isForwarded"] and not p["forwardedFrom"]}
if not broken:
break
await asyncio.sleep((5, 10, 15)[attempt])
again = parse_page(await fetch(page_url))
# keep the version of each broken post where the source is filled in
Two rules made it usable:
- Only do this for the seed channels, where a lost reference hurts the most.
- If a page cannot be repaired at all, stop retrying for that channel. Otherwise permanently empty forwards turn a 20 second run into minutes.
Result
Before: 6, 2, 6, 2. After: 6, 6, 6, 6, and in one of those runs the empty header appeared and the retry repaired it. The price is a slower run, about 50 seconds instead of 20.
It is not perfect. If Telegram takes longer than 30 seconds, the reference is still lost.
Takeaways
- If your scraper is unstable, log one concrete item in a good run and a bad run and compare them. I lost time guessing before I did this.
- An HTTP 200 with valid HTML can still be incomplete.
- Retry on missing data, not only on errors.
The scraper is Telegram Similar Channels Finder on Apify. I debugged this together with an AI assistant.
If you know a more reliable way to get the forward source from the preview, please tell me.
Top comments (0)