對Requests和sqlite3進行封裝

封裝了兩個庫,簡化一下代碼。第一個不是最終版,先用著,比如說,L30那里關于請求的參數,只列出了常用的幾個。到時候有需求了再改。

ohRequests.py

這個封裝會對請求失敗的URL默認重新請求3次,避免網絡波動導致抓取失敗。

import requests
from faker import Faker


class ohRequests(object):
    def __init__(self, retries = 1):
        self.faker = Faker()
        self.GET = 'get'
        self.POST = 'post'
        self.RETRIES = retries

    def __request(self,
                method,
                url,
                headers = {},
                cookies = None,
                proxies = None,
                params  = None,
                data    = None,
                timeout = 5):

        # FIXME: 
        # 沒有檢測User-Agent, user-Agent之類的key名
        headers['user-agent'] = self.faker.user_agent()

        for i in range(self.RETRIES):
            req_methond = requests.post if method == 'post' else requests.get
            response = None
            try:
                response = req_methond(url, 
                                headers = headers,
                                cookies = cookies,
                                proxies = proxies,
                                data    = data,
                                timeout = timeout, 
                                params  = params
                            )
                response.encoding = 'utf-8'
                return response

            except Exception:
                print ('HTTPError, Status Code: {}'.format(response.status_code))

            print ("Requests Error. Retry[{}], max retries[{}]".format(i+1, self.RETRIES))

        return None

    def get(self, url, **kwargs):
        return self.__request('get', url, **kwargs)

    def post(self, url, **kwargs):
        return self.__request('post', url, **kwargs)

    def faker_user_agent(self):
        return self.faker.user_agent()

if __name__ == '__main__':
    r = ohRequests()
    req = r.get("http://www.baidu.com")
    print (req)

ohSqlite3.py

import sqlite3
import os

class ohSqlite3(object):
    """ APIs for using Sqlite3.

    Attributes:
        dbpath: the db file path for manipulation
        conn: connection object returned from sqlite3.connect()
        cursor: cursor object returned from conn

        SHOW_SQL_STATEMENT: whether print sql statement, default False
    """

    SHOW_SQL_STATEMENT = False

    def __init__(self, dbpath):
        """inits ohSqlite3 with dbpath"""
        self.dbpath = dbpath
        self.conn = None
        self.cursor = None

    def __enter__(self):
        self.connect()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()

    def connect(self):
        """init db connection
        
        Raises:
            sqlite3.Error: An error occurred when attempting to connect sqlite db.
        """
        try:
            self.conn = sqlite3.connect(self.dbpath)
        except sqlite3.Error as e:
            print ("Connect {} failed. Error: {}".foramt(self.dbpath, e.args[0]))
        
        self.cursor = self.conn.cursor()

    def execute(self, sql, args = None):
        """execute sql statement

        Args:
            sql: the sql statement to be executed
            args: a tuple of fields to be added to sql, default None

        Returns:
            result of sql statement execution

        Raises:
            Exception: any errors occurred when executing sql statement

        """
        if self.SHOW_SQL_STATEMENT:
            print ("[SQL]{}".format(sql))

        data = None
        try:
            if args:
                self.cursor.execute(sql, args)
            else:
                self.cursor.execute(sql)
            data = self.cursor.fetchall()
        except Exception as e:
            print (e, "[SQL]:" + sql.strip())
            self.conn.rollback()

        self.conn.commit()
        if data:
            return data
        return None

    def query(self, sql, args = None):
        """Same as execute(), different names"""
        return self.execute(sql, args = None)

    def close(self):
        """close all objects"""
        self.cursor.close()
        self.conn.close()

def unitest():
    testdb = "ohSqlite3_test.db"

    # test funcs
    db = ohSqlite3(testdb)
    db.connect()

    db.execute("CREATE TABLE stocks (date text, trans text, symbol text, qty real, price real)")

    db.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
    db.execute("INSERT INTO stocks VALUES ('2006-03-28', 'BUY', 'IBM', 1000, 45.00)")
    db.execute("INSERT INTO stocks VALUES ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00)")
    db.execute("INSERT INTO stocks VALUES ('2006-04-06', 'SELL', 'IBM', 500, 53.00)")

    assert 4 == db.execute("SELECT count(*) FROM stocks")[0][0]

    db.execute("DROP TABLE stocks")

    db.close()

    # test __with__ statement
    with ohSqlite3(testdb) as db:
        db.query("CREATE TABLE stocks (date text, trans text, symbol text, qty real, price real)")

        db.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
        db.execute("INSERT INTO stocks VALUES ('2006-03-28', 'BUY', 'IBM', 1000, 45.00)")
        db.execute("INSERT INTO stocks VALUES ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00)")
        db.execute("INSERT INTO stocks VALUES ('2006-04-06', 'SELL', 'IBM', 500, 53.00)")

        assert 4 == db.execute("SELECT count(*) FROM stocks")[0][0]

        db.execute("DROP TABLE stocks")

    os.remove(testdb)


if __name__ == "__main__":
    unitest()
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,932評論 18 139
  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網絡請求組件 FMDB本地數據庫組件 SD...
    陽明AGI閱讀 16,009評論 3 119
  • 奇怪的是,不知為什么,加我小的時候,我極力裝得像大人;而在我已經不是小孩的時候,我又希望像個孩子。 ...
    虞靜呀閱讀 330評論 0 2
  • 今晚兒子做完作業,本想帶他出去散步,兒子讓我陪他做游戲,算了,明天再出去吧。 做完游戲趕緊讓小家伙...
    寶鈞閱讀 156評論 0 6
  • 幾點說明: 1.這是一部已經完結的小說,因此,如果沒有極特殊情況,絕對不會棄坑不填。2.為網發時的清晰明了,我特意...
    葉初夏閱讀 516評論 19 17