爬取百度暗影精灵5贴吧

导入模块

import requests,os

定义函数编写参数

def get_page(Kw,pn,filename)
    params={
        'ie':utf-8,
        'Kw':'kw',
        'pn':'pn',
    }
base_url='https://tieba.baidu.com/f?ie=utf-8&kw=%E6%9A%97%E5%BD%B1%E7%B2%BE%E7%81%B55&fr=search' 

发送请求获取响应

response=request.get(url=base_url,params=params)

保存数据

with open(filename,'w',encoding='utf-8') as fp: 
        fp.write(response.text)

测试运行

if __name__ == '__main__':

编写分页

 for i in range(10):
    pn=i*50
    filename=r'./tieba/'+kw
    if not os.path.exists(filename):
       os.mkdir(filename)
    get_page(kw,pn,filename+'/{}.html'.format(i+1))

猜你喜欢

转载自blog.csdn.net/h1751541643/article/details/109162422
今日推荐