hzm1815007 2021-10-20 17:03 采纳率: 85.7%
浏览 21

mysql8.0 使用mysql.connector模块, 连接不了数据库,不可用,存储失败,找不到原因

使用 pymysql 模块正常 读取和存储

img

#! /usr/bin/env python
#-*- encoding: utf-8 -*-


import requests
import re
import pymysql
import time
import mysql.connector # mysql 8.0 连接模块

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}

def baidu(company):
    # 1.获取网页源代码(参考2.3、3.1、3.4节)
    url = 'https://www.baidu.com/s?rtt=1&bsst=1&cl=2&tn=news&ie=utf-8&word=' + company  # 其中设置rtt=4则为按时间排序,如果rtt=1则为按焦点排序
    res = requests.get(url, headers=headers, timeout=10).text
    # 2.编写正则提炼内容(参考3.1节)
    p_href = '<h3 class="news-title_1YtI1">.*?<a href="(.*?)"'
    p_title = '<a href=.*?class="news-title-font_1xS-F".*?aria-label="(.*?)".*?</a>'
    p_info = '<span class="c-color-gray c-font-normal c-gap-right".*?>(.*?)</span>'
    p_date = '<span class="c-color-gray2 c-font-normal".*?>(.*?)</span>'

    href = re.findall(p_href, res, re.S)
    title = re.findall(p_title, res, re.S)
    info = re.findall(p_info, res, re.S)
    source = info
    date = re.findall(p_date, res, re.S)

    # 3.数据清洗(参考3.1节)

    for i in range(len(title)):
        title[i] = title[i].strip()
        title[i] = re.sub('标题:', '', title[i])


    score = []

    # 6.将数据存入数据库及数据去重(参考4.4节和5.1节)
    for i in range(len(title)):
        print(title[i])
        db = mysql.connector.connect(host='localhost', port=3308, user='root', password='x', database='pachong',
                             charset='utf8')
        cur = db.cursor()  # 获取会话指针,用来调用SQL语句

        # 6.1 查询数据,为之后的数据去重做准备
        sql_1 = 'SELECT * FROM article WHERE company =%s'
        cur.execute(sql_1, company)
        data_all = cur.fetchall()
        print(data_all)
        title_all = []
        for j in range(len(data_all)):
            title_all.append(data_all[j][1])

        # 6.2 判断数据是否在原数据库中,不在的话才进行数据存储
        if title[i] not in title_all:
            sql_2 = 'INSERT INTO article(company,title,href,source,date,score) VALUES (%s,%s,%s,%s,%s,%s)'  # 编写SQL语句
            print(title[i])
            print(source[i])
            # 执行SQL语句
            cur.execute(sql_2, (company, title[i], href[i], source[i], date[i], score[i]))
            db.commit()  # 当改变表结构后,更新数据表的操作
        cur.close()  # 关闭会话指针
        db.close()  # 关闭数据库链接
    print('------------------------------------')  # 分割符


def main():
    # 7.批量爬取多家公司(参考3.2节)
    companys = ['华能信托', '阿里巴巴', '百度集团']
    for company in companys:
        try:
            baidu(company)
            print(company + '数据爬取并存入数据库成功')
        except:
            print(company + '数据爬取并存入数据库失败')
main()


  • 写回答

1条回答 默认 最新

  • 辉煌仪奇 2021-10-20 17:09
    关注

    sql语句模板中的参数填充符是 %s 而不是 ‘%s’ ,且多个参数需要用元祖存放,单个参数可直接传递,将你的模板语句做如下改动即可

    sql_1 = "SELECT * FROM article WHERE company ='%s'"
    
    sql_2 ="'INSERT INTO article(company,title,href,source,date,score) VALUES ('%s','%s','%s','%s','%s','%s')"  # 编写SQL语句
    

    有帮助请采纳,有问题继续交流,你的采纳是对我回答的最大的肯定和动力

    评论

报告相同问题?

问题事件

  • 创建了问题 10月20日

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能