为了账号安全,请及时绑定邮箱和手机立即绑定

之前好好的,重新安装了数据库,然后就报错了,望帮忙看看呢。谢谢

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/kelvintong/Documents/pycode/demo1/wiki2mysql.py

Wikipedia <----> https://en.wikipedia.org/wiki/Wikipedia

Traceback (most recent call last):

  File "/Users/kelvintong/Documents/pycode/demo1/wiki2mysql.py", line 35, in <module>

    cursor.execute(sql,(url.get_text(),"https://en.wikipedia.org"+ url["href"]))

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/cursors.py", line 166, in execute

    result = self._query(query)

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/cursors.py", line 322, in _query

    conn.query(q)

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py", line 852, in query

    self._affected_rows = self._read_query_result(unbuffered=unbuffered)

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py", line 1053, in _read_query_result

    result.read()

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py", line 1336, in read

    first_packet = self.connection._read_packet()

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py", line 1010, in _read_packet

    packet.check_error()

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py", line 393, in check_error

    err.raise_mysql_exception(self._data)

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/err.py", line 107, in raise_mysql_exception

    raise errorclass(errno, errval)

pymysql.err.InternalError: (1364, "Field 'id' doesn't have a default value")

Process finished with exit code 1


正在回答

4 回答

右键点击urls表,选设计表,然后点下面的自动增长,我就是这样可以了

0 回复 有任何疑惑可以回复我~
pymysql.err.InternalError: (1364, "Field 'id' doesn't have a default value")

id列没有默认值。试试在mysql的表中,id列设置为自动增长autoincrement。

0 回复 有任何疑惑可以回复我~

我能说,我用你的代码执行,是正确的,数据库里已经正常存入了。

#! /usr/local/bin/python3
# -*- coding:utf-8 -*-

from urllib.request import urlopen
from bs4 import BeautifulSoup as bs
import re
import pymysql.cursors

resp = urlopen("https://en.wikipedia.org/wiki/Main_Page").read().decode(
    "utf-8")

soup = bs(resp, "html.parser")

listUrls = soup.find_all("a", href=re.compile("^/wiki/"))
for url in listUrls:
    if not re.search("\.(jpg|JPG)$", url["href"]):
        print(url.get_text("href"), "<---->",
              "https://en.wikipedia.org" + url["href"])
        connection = pymysql.connect(host='localhost',
                                     user='root',
                                     password='123456',
                                     db='wikiurl',
                                     charset='utf8mb4')
        try:
            with connection.cursor() as cursor:
                sql = "insert into `urls`(`urlname`,`urlhref`)values(%s,%s)"

                cursor.execute(sql, (
                url.get_text(), "https://en.wikipedia.org" + url["href"]))

                connection.commit()

        finally:
            connection.close()

http://img1.sycdn.imooc.com//58be83c80001e97813370645.jpg

0 回复 有任何疑惑可以回复我~

#! /usr/local/bin/python3
# -*- coding:utf-8 -*-

# 引入开发包
from urllib.request import urlopen
from bs4 import BeautifulSoup as bs
import re
import pymysql.cursors

# 请求URL并把结果用UTF-8编码
resp = urlopen("https://en.wikipedia.org/wiki/Main_Page").read().decode("utf-8")

# 使用beautifulsoup去解析
soup = bs(resp,"html.parser")

# 获取所有以/wiki/开头的a标签的href属性
listUrls = soup.find_all("a", href=re.compile("^/wiki/"))
for url in listUrls:
   # 过滤-.jpg或JPG结尾的URL
   if not re.search("\.(jpg|JPG)$",url["href"]):
       # 输出URL的文字和对应的链接
       print(url.get_text("href"),"<---->","https://en.wikipedia.org"+url["href"])
       #获取数据库链接
       connection = pymysql.connect(host = 'localhost',
                                    user = 'root',
                                    password = '123456',
                                    db = 'wikiurl',
                                    charset = 'utf8mb4')
       try:
           # 获取回话指针
           with connection.cursor() as cursor:
               sql = "insert into `urls`(`urlname`,`urlhref`)values(%s,%s)"

               # 执行sql语句
               cursor.execute(sql,(url.get_text(),"https://en.wikipedia.org"+ url["href"]))

               # 提交
               connection.commit()


       finally:
           connection.close()

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

之前好好的,重新安装了数据库,然后就报错了,望帮忙看看呢。谢谢

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信