和你的代码一样,为什么我执行mysql插入时花费的时间
[Finished in 164.8s]
你把插入数据库的代码注释掉,然后只是打印名字和网址,看看需要多久。
代码一样,这个是数据库配置不对吗
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 '~urls~(~urlname~, ~urlhref~)values('Wikipedia', 'https://en.wikipedia.org/wiki/W' at line 1")
values = [] # 输出所有的词条对应的名称和URL属性 for url in listUrls: #过滤.jpg或.JPG结尾的URL if not re.search("\.(jpg|JPG)$",url["href"]): #输出URL的文字和对应的链接 print(url.get_text(),"<----->","https://en.wikipedia.org"+url["href"]) values.append((url.get_text(),"https://en.wikipedia.org"+url["href"])) try: connection = pymysql.connect(host="localhost",user="root",password="root",db="wikiurl",charset="utf8mb4") with connection.cursor() as cursor: sql = "insert into `urls`(`urlname`,`urlhref`)values(%s,%s)" cursor.executemany(sql,values) connection.commit() except: info = sys.exc_info() print(info) print(info[0]) print(info[1]) finally: connection.close()
上面代码我换了思路,把mysql操作放在最后来做,同时调用cursor.executemany, 一次可以插入多条记录