cursor.execute报错,代码和老师的一样
MySQL版本5.7.17,Python版本3.6.0,鼓捣一下午都没弄出来

MySQL版本5.7.17,Python版本3.6.0,鼓捣一下午都没弄出来

2017-03-20
import io
import sys
import re
import pymysql.cursors
from urllib.request import urlopen
from bs4 import BeautifulSoup as bs
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')
# 请求URL,并把结果用UTF-8编码
resp = urlopen("https://en.wikipedia.org/wiki/Wiki").read().decode('utf-8')
# 使用BeautifulSoup去解析
soup = bs(resp, "html.parser")
# 获取所有以/wiki/开头的a标签的href属性
listurls = soup.find_all('a', href=re.compile("^/wiki/"))
# 输出所有的词条对应的名称和URL
for url in listurls:
# 过滤以.jpg或.JPG结尾的URL
if not re.search("\.(jpg|JPG)$", url["href"]):
# 输出URL的文字对应的链接
# string只能获取一个 get_text()获取标签下所有的文字
print(url.get_text(),"<---->","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语句
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()# 引入开发包
from urllib.request import urlopen
from bs4 import BeautifulSoup as bs
import re
import pymysql.cursors
# 请求URL,并把结果用UTF-8编码
resp = urlopen("https://zh.wikipedia.org/wiki/Wikipedia:%E9%A6%96%E9%A1%B5").read().decode("utf-8")
# 使用BeautifulSoup去解析
soup = bs(resp, "html.parser")
# 获取所有以/wiki/开头的a标签的href属性
listUrls = soup.findAll("a", href=re.compile("^/wiki/"))
# 输出所有的词条对应的名称和URL
for url in listUrls:
# 过滤以.jpg或.JPG结尾的URL
if not re.search("\.(jpg|JPG)$", url["href"]):
# 输出URL的文字对应的链接
# string只能获取一个 get_text()获取标签下所有的文字
print(url.get_text(), "<---->", "https://zh.wikipedia.org" + url["href"])
# 获取数据库链接
connection = pymysql.connect(host='localhost',
user='root',
password='root',
db='wikiurl',
charset='utf8mb4')
try:
# 获取会话指针
with connection.cursor() as cursor:
# 创建sql语句
sql = "inset into `urls` (`urlname`, `urlhref`) values(%s, %s)"
# 执行sql语句
cursor.execute(sql, (url.get_text(), "https://zh.wikipedia.org" + url["href"]))
# 提交
connection.commit()
finally:
connection.close()举报