有14条数据,然后报了‘pymysql.err.Error: Already closed’这个错误

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

qq_luoRengui_0

2017-07-29 09:11

#coding=utf-8


import urllib

from bs4 import BeautifulSoup

import re

import pymysql.cursors



resp = urllib.urlopen('https://en.wikipedia.org/wiki/Main_Page').read().decode('utf-8')

soup = BeautifulSoup(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']):

print url.get_text(), '<======>','https://en.wikipedia.org'+url['href']

# 获取数据库连接

connection = pymysql.connect(host = 'localhost',

user = 'root',

password = '6322004',

db = 'wikiurl',

charset = 'utf8mb4')


try:

# 获取会话指针

with connection.cursor() as cursor:

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


写回答 关注

2回答

  • 昔日大神不复存在
    2020-12-16 19:06:06

    finally:

    connection.close()

    把这块去掉

  • 慕仰0192323
    2017-07-29 18:00:31

    你缩进就是这样的吗?

python遇见数据采集

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

59669 学习 · 200 问题

查看课程

相似问题