之前好好的,重新安裝了數據庫,然后就報錯了,望幫忙看看呢。謝謝
/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
2018-03-22
右鍵點擊urls表,選設計表,然后點下面的自動增長,我就是這樣可以了
2017-05-04
id列沒有默認值。試試在mysql的表中,id列設置為自動增長autoincrement。
2017-03-07
我能說,我用你的代碼執行,是正確的,數據庫里已經正常存入了。
2017-02-20
#! /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()