爬虫实战--爬取百度贴吧

一样先看网址:
在这里插入图片描述
根据规律来写代码

import requests

kw = "python"
url_sample = "http://tieba.baidu.com/f?kw="+kw+"&ie=utf-8&pn={}"
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"}


for i in range(1,11):
    url = url_sample.format((i-1)*50)
    re = requests.get(url, headers=headers).content.decode()
    file_path = "{}贴吧第{}页.html".format(kw,i)
    with open(file_path, "w", encoding="utf-8") as f:
        f.write(re)
    f.close()

结果:
在这里插入图片描述

发布了125 篇原创文章 · 获赞 56 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/Nicht_sehen/article/details/102177407