qq_34469410
qq_34469410
2017-05-09 08:25

Python3.5连接pymysql出现cursor问题

10
  • python
  • mysql

Ubuntu下Python3.5连接pymysql时出现错位为 with connect.cursor() as cursor:
AttributeError: 'function' object has no attribute 'cursor'
以下为代码,点击错误跳到with connect.cursor() as cursor这里,不知道为什么,弄了两天了都没弄好,求大神告诉

-*- coding:utf-8 -*-

import urllib
import urllib.request
import re
import random
import pymysql.cursors
#抓取所需内容
from pymysql import connect

user_agent = ["Mozilla/5.0 (Windows NT 10.0; WOW64)", 'Mozilla/5.0 (Windows NT 6.3; WOW64)',
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko',
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36',
]
stock_total=[] #stock_total:所有页面的股票数据 stock_page:某页的股票数据
url='http://quote.stockstar.com/stock/ranklist_a_3_1_1.html'
#伪装浏览器请求报头
request=urllib.request.Request(url=url,headers={"User-Agent":random.choice(user_agent)})
try:
response=urllib.request.urlopen(request)
except urllib.error.HTTPError as e: #异常检测
print(e.code)
except urllib.error.URLError as e:
print(e.reason)
content=response.read().decode('gbk') #读取网页内容
#打印成功获取的页码
pattern=re.compile('

')
body=re.findall(pattern,str(content))
pattern=re.compile('>(.*?)<')
stock_page=re.findall(pattern,body[0]) #正则匹配
stock_total.extend(stock_page)

#删除空白字符
stock_last=stock_total[:] #stock_last为最终所要得到的股票数据
for data in stock_total:
if data=='':
stock_last.remove('')
print('1')

db = pymysql.Connect(
host='localhost',
user='root',
passwd='111111',
db='patest1',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor

)

try:
with connect.cursor() as cursor:
sql="insert into pachong values (%s, %s, %s, %s, %s)"

    param=[(stock_last[0]),(stock_last[1]),(stock_last[2]),(stock_last[3]),(stock_last[4]),(stock_last[5])]
    n=cursor.executemany(sql,param)
connect.commit()

finally: print('n')
connect.close()

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答