欢迎光临
感谢一路有你

python 通用爬虫(百度贴吧)

import requests

class TiebaSpider():
    def __init__(self,tieba_name):
        self.tieba_name = tieba_name
        print(self.tieba_name)
        self.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_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"}
    def get_url_list(self):
        # url_list = []
        # for i in range(1000):
        #     url_list.append(self.url_temp.format(i*50))
        # return url_list
        # 等同于如下
        return [self.url_temp.format(i*50) for i in range(1000) ]
    def parse_url(self,url):
        print(url)
        response = requests.get(url,headers = self.headers )
        return response.content.decode()

    def save_html(self,html_str,page_num):
        tieba_name = self.tieba_name
        file_path = "{}-{}页.HTML".format(tieba_name,page_num)
        with open(file_path,"w",encoding="utf-8") as f:
            f.write(html_str)

    # 1.构建url列表
    #2.遍历发布请求
    #3.保存
    def run(self):
        url_list = self.get_url_list()

        for url in url_list:
            html_str = self.parse_url(url)
            page_num = url_list.index(url)+1
            self.save_html(html_str,page_num)



if __name__ == '__main__':
    tieba_spider = TiebaSpider("李毅")
    tieba_spider.run()


赞(0) 打赏
未经允许不得转载:王明昌博客 » python 通用爬虫(百度贴吧)
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏