1. 程式人生 > >認識爬蟲(2)

認識爬蟲(2)

用爬蟲下載簡單的頁面,可以拿來練手

import requests

class TiebaSpider:
    def __init__(self,tieba_name_crawl):
        """
        初始化必要引數,完成基礎設定
        :param tieba_name_crawl:
        """
        self.tieba_name = tieba_name_crawl
        self.url_base = 'https://tieba.baidu.com/f?kw=' + tieba_name_crawl + '&ie=utf-8&pn={}'
        self.headers = {'User_Agent':'WSF'}

    def make_url_list(self):
        """
        生成下載列表
        :return: 下載列表
        """
        return [self.url_base.format(i*50) for i in range(4)]

    def download_url(self,url_str):
        """
        使用requests get方法下載指定頁面,並返回頁面結果
        :param url_str: 下載連結
        :return: 下載結果
        """
        result = requests.get(url_str,headers = self.headers)
        return result.content

    def save_result(self,result,page_num):
        """
        儲存下載內容
        :param result:
        :param page_num:
        :return:
        """
        file_path = "{}-第{}頁碼.html".format(self.tieba_name,page_num)
        with open(file_path,'wb') as f:
            f.write(result)

    def run(self):
        """
        下載主執行緒,實現主要的下載邏輯
        :return:
        """
        url_lists = self.make_url_list()
        print(url_lists)
        for url_str in url_lists:
            result_str = self.download_url(url_str)
            p_num      = url_lists.index(url_str) + 1
            self.save_result(result_str,p_num)


if __name__ == '__main__':
    tieba_spider = TiebaSpider('lol')
    tieba_spider.run()