如何修改使之能够将结果保存在MySQL中
请问,刚刚的是存贮在文件里面的,可要是要存储在MySQL这种关系型数据库中的话,要怎么修改呢,最后的代码~
请问,刚刚的是存贮在文件里面的,可要是要存储在MySQL这种关系型数据库中的话,要怎么修改呢,最后的代码~
2016-12-27
for data in self.datas: connection = pymysql.connect(host='localhost', user='root', password='********', #密码 db='*******', #数据库 charset='utf8mb4') try: with connection.cursor() as cursor: sql = 'insert into `db`(`url`,`urltitle`,`summary`) values(%s,%s,%s)' cursor.execute(sql,(data['url'],data['title'].encode('utf-8'),data['summary'].encode('utf-8'))) connection.commit() finally: connection.close()
Traceback (most recent call last):
File "E:\java\python pachong\python pachong\baike_python\main_python.py", line 34, in <module>
obj_spider.craw(root_url)
File "E:\java\python pachong\python pachong\baike_python\main_python.py", line 30, in craw
self.outputer.output_html()
File "E:\java\python pachong\python pachong\baike_python\html_outputer.py", line 34, in output_html w
ith connection.cursor() as cursor:
AttributeError: __exit__
一直出现这种问题,请问怎么解决,还望指教QAQ!
举报