Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MysqlDB - feapder-document #29

Open
Boris-code opened this issue Mar 15, 2021 · 3 comments
Open

MysqlDB - feapder-document #29

Boris-code opened this issue Mar 15, 2021 · 3 comments

Comments

@Boris-code
Copy link
Owner

https://boris.org.cn/feapder/#/source_code/MysqlDB

feapder是一款支持分布式、批次采集、任务防丢、报警丰富的python爬虫框架

@Lu-dashuai
Copy link

大佬 数据库连接不支持 ssl连接

@qqizai
Copy link

qqizai commented Nov 16, 2021

大佬,to_json 的时候,好像没判断是否结果为空,会导致报错。你查一下,我也不是很确定,具体是:

@auto_retry
    def find(self, sql, limit=0, to_json=False):
        """
        @summary:
        无数据: 返回()
        有数据: 若limit == 1 则返回 (data1, data2)
                否则返回 ((data1, data2),)
        ---------
        @param sql:
        @param limit:
        @param to_json 是否将查询结果转为json
        ---------
        @result:
        """
        conn, cursor = self.get_connection()

        cursor.execute(sql)

        if limit == 1:
            result = cursor.fetchone()  # 全部查出来,截取 不推荐使用
        elif limit > 1:
            result = cursor.fetchmany(limit)  # 全部查出来,截取 不推荐使用
        else:
            result = cursor.fetchall()

        if to_json:  # todo 这里下面
            columns = [i[0] for i in cursor.description]

            # 处理数据
            def fix_lob(row):
                def convert(col):
                    if isinstance(col, (datetime.date, datetime.time)):
                        return str(col)
                    elif isinstance(col, str) and (
                        col.startswith("{") or col.startswith("[")
                    ):
                        try:
                            # col = self.unescape_string(col)
                            return json.loads(col)
                        except:
                            return col
                    else:
                        # col = self.unescape_string(col)
                        return col

                return [convert(c) for c in row]

            result = [fix_lob(row) for row in result]  # todo 这里 result 如果为None,会报错吧
            result = [dict(zip(columns, r)) for r in result]

        self.close_connection(conn, cursor)

        return result

@suyin-long
Copy link

查看指定的MySQL数据库中有哪些数据表?

from feapder.db.mysqldb import MysqlDB

db = MysqlDB.from_url("mysql:https://username:password@ip:port/db?charset=utf8mb4")
# 查询指定数据库下的所有数据表
tables = [tb[0] for tb in db.find("show tables;")]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants