执行代码出错,卿帮忙找一下原因

来源:4-1 存储数据到MySQL

xuanbg163

2016-10-18 19:01

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")


写回答 关注

4回答

  • 初学程序
    2017-09-27 07:50:48

    解决了:

    sqll="insert into  testurl (urlname, urlhref)values(%s,%s)"

    按照上面这样修改即可,注意testurl后面的空格

  • 初学程序
    2017-09-27 06:45:26

    我也想知道,为什么会这样子呢?

  • Thpffcj
    2017-01-18 14:33:56

    请问解决了么?

  • difoi
    2016-10-18 23:06:21

    sql 语法错误.

python遇见数据采集

本教程让你初步掌握Python进行数据采集,创造属于你的价值

59669 学习 · 200 问题

查看课程

相似问题