from urllib.request import urlopen
from bs4 import BeautifulSoup as bs
import re
import pymysql
resp=urlopen("https://en.wikipedia.org/wiki/Main_Page").read().decode("utf-8")
soup=bs(resp,"html.parser")
listurls=soup.findAll("a", href=re.compile("^/wiki/"))
#print(listurls)
for url in listurls:
if not re.search("\.(jpg|JPG)",url["href"]):
print(url.get_text(),"<------->","https://en.wikipedia.org"+url["href"])
conn = pymysql.connect(host="localhost",
user="root",
password="xuanbg126",
db="wikiurl",
charset="utf8mb4" )
try:
with conn.cursor()as cur:
sqll="insert into 'testurl'('urlname','urlhref')values(%s,%s)"
cur.execute(sqll,(url.get_text(),"https://en.wikipedia.org"+ url["href"]))
conn.commit()
#except HandlException as e:
#conn.rollback()
finally:
conn.close()
errr ifn:
Traceback (most recent call last):
File "C:/Users/morgan/Desktop/bao2/feq1.py", line 22, in <module>
cur.execute(sqll,(url.get_text(),"https://en.wikipedia.org"+ url["href"]))
File "C:\Program Files\Python35\lib\site-packages\pymysql\cursors.py", line 166, in execute
result = self._query(query)
File "C:\Program Files\Python35\lib\site-packages\pymysql\cursors.py", line 322, in _query
conn.query(q)
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 835, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 1019, in _read_query_result
result.read()
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 1302, in read
first_packet = self.connection._read_packet()
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 981, in _read_packet
packet.check_error()
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "C:\Program Files\Python35\lib\site-packages\pymysql\err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''testurl'('urlname','urlhref')values('Disclaimers','https://en.wikipedia.org/wik' at line 1")
解决了:
sqll="insert into testurl (urlname, urlhref)values(%s,%s)"
按照上面这样修改即可,注意testurl后面的空格
我也想知道,为什么会这样子呢?
请问解决了么?
sql 语法错误.