爬虫--爬取网页信息存储到Mysql数据库

本文介绍两种在MySQL中存储特殊内容的方法:使用repr函数转换数据和使用元组传递参数。通过实例展示了如何从网络获取数据并安全地存入数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有两种方法进行处理特殊内容的存储:

第一种:使用 repr 函数

               repr() 函数将对象转化为供解释器读取的形式。

import json
import requests
import pymysql


class mysql_conn(object):
    # 魔术方法 ,初始化 , 构造函数
    def __init__(self):
        self.db = pymysql.connect(host='127.0.0.1',user='root',password='123456',port=3306,database='python')
        self.cursor = self.db.cursor()
    # 执行modify(修改)相关的操作
    def execute_modify_mysql(self,sql):
        self.cursor.execute(sql)
        self.db.commit()
    # 魔术方法 , 析构化 , 析构函数
    def __del__(self):
        self.cursor.close()
        self.db.close()

mc = mysql_conn()

headers = {
    'Cookie': '***********',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
}

# urllib 的相关操作如下
url = 'https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id=-1&count=10&category=111'

response = requests.get(url, headers=headers)

res_dict = json.loads(response.text)

list_list = res_dict['list']

for list_item_dict in list_list:
    # list 列表内的一个item, 他是一个dict
    data_str = list_item_dict['data']

    data_dic = json.loads(data_str)

    id = int(data_dic['id'])
    title = data_dic['title']
    description = data_dic['description']
    target = data_dic['target']
    sql = "insert into xue_sql(id,title,description,target) values (%d,%s,%s,%s);"%(id,repr(title),repr(description),repr(target))
    mc.execute_modify_mysql(sql)



第二种方法:

                 使用 元组 的形式把值进行传递,拼接

import json
import requests
import pymysql


class mysql_conn(object):
    # 魔术方法 ,初始化 , 构造函数
    def __init__(self):
        self.db = pymysql.connect(host='127.0.0.1',user='root',password='123456',port=3306,database='python')
        self.cursor = self.db.cursor()
    # 执行modify(修改)相关的操作
    def execute_modify_mysql(self,sql,data = None):
        self.cursor.execute(sql,data)
        self.db.commit()
    # 魔术方法 , 析构化 , 析构函数
    def __del__(self):
        self.cursor.close()
        self.db.close()

mc = mysql_conn()

headers = {
    'Cookie': '*******',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
}

# urllib 的相关操作如下
url = 'https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id=-1&count=10&category=111'

response = requests.get(url, headers=headers)

res_dict = json.loads(response.text)

list_list = res_dict['list']

for list_item_dict in list_list:
    # list 列表内的一个item, 他是一个dict
    data_str = list_item_dict['data']

    data_dic = json.loads(data_str)

    id = int(data_dic['id'])
    title = data_dic['title']
    description = data_dic['description']
    target = data_dic['target']

    data = (id,repr(title),repr(description),repr(target))

    sql = "insert into new_xue(id,title,description,target) values (%s,%s,%s,%s);"
    mc.execute_modify_mysql(sql,data = data)



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值