如何使用 Python 和 requests 下载论坛页面为 HTML

这将以降序下载第 1000 页…第 1 页,并将 HTML 保存为 1000.html1.html

download_forum_pages.py
import requests
for i in range(1000, 0, -1):
    with open(f"{i}.html", "w") as outfile:
        outfile.write(requests.get(f"https://forum.my-domain.com/showthread.php?123456-my-thread/page{i}").text)

带进度条

download_forum_pages_with_progress.py
import requests
from tqdm import tqdm
for i in tqdm(range(1000, 0, -1)):
    with open(f"{i}.html", "w") as outfile:
        outfile.write(requests.get(f"https://forum.my-domain.com/showthread.php?123456-my-thread/page{i}").text)

Check out similar posts by category: Python