import requests cnt = 0 while True: cnt += 1 req = requests.get('https://zh.wikipedia.org/wiki/Special:Random?uselang=zh-tw') html = req.text print(cnt) if '
維基百科,自由的百科全書
' not in html: break with open('temp.html', 'w', encoding='utf8') as f: f.write(html)