#coding=utf-8importrequestsclassTiebaSPider:def__init__(self,tieba_name):self.tieba_name=tieba_nameself.url_temp="https://tieba.baidu.com/f?kw="+tieba_name+"&ie=utf-8&pn={}"self.headers={"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"}"""1.获取到一个url列表步骤a建立空列表url_list,步骤b从1到1000开始给网址self.url_temp填入数据格式信息format(i*50)"""defget_url_list(self):#url_list=[]#foriinrange(1000):#url=self.url_temp.format(i*50)#url_list.append(url)url_list=[self.url_temp.format(i*50)foriinrange(1000)]returnurl_list#定义函数defparse_url(self,url):#发送请求,获取响应print("现在正在请求:",url)r=requests.get(url,headers=self.headers)#对返回变量r的字节码进行解码操作returnr.content.decode()defsave_html_str(self,html_str,page_number):file_path="{}_第{}页.html".format(self.tieba_name,page_number)withopen(file_path,"w",encoding="utf-8")asf:f.write(html_str)print("保存成功")defrun(self):#实现我们的主要逻辑#1.urllisturl_list=self.get_url_list()#2.遍历url,发送请求,获取响应forurlinurl_list:html_str=self.parse_url(url)#3.保存page_number=url_list.index(url)+1self.save_html_str(html_str,page_number)search=input("请输入贴吧中文名字:")if__name__=='__main__':#给类TiebaSPider传入参数,贴吧名,同时调用类新实例名称:tieba_spider#调用实例的方法run()tieba_spider=TiebaSPider(search)tieba_spider.run()