pyspider 连接mysql_pyspider 导入模块报错 提示 No module named mysqldb
学习pyspider 参照《 把 pyspider的结果存入自定义的mysql数据库中 》文章
设置了本地的mysql数据库及连接,但运行时 提示 :process csair:_on_get_info data:,_on_get_info -> [200] len:12 -> result:None fol:0 msg:0 err:ImportError(‘No module named mysqldb’,)
系统环境:centos7.1
mysql-python(1.2.5) pip 默认安装
python采用默认的2.7.5
希望得到帮助 感谢
文件已存到 pyspider/database/mysql/mysqldb.py 脚本文件如下:
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2015-12-21 09:28:49
# Project: csair
from pyspider.libs.base_handler import *
from pyspider.database.mysql.mysqldb import SQL
class Handler(BaseHandler):
crawl_config = {
}
@every(minutes=24 * 60)
def on_start(self):
self.crawl('http://www.csair.cn/cn/pages/NewsList.aspx?listname=%D6%D8%D2%AA%B9%AB%B8%E6', callback=self.index_page)
@config(age=10 * 24 * 60 * 60)
def index_page(self, response):
for each in response.doc('a[href^="http"]').items():
self.crawl(each.attr.href, callback=self.detail_page)
def list_page(self, response):
for each in response.doc('ul_list7 a').items():
self.crawl(each.attr.href, callback=self.detail_page)
for each in response.doc('.currentnumeric').items():
self.crawl(each.attr.href, callback=self.list_page)
@config(priority=2)
def detail_page(self, response):
return {
"url": response.url,
"title": response.doc('ctl00_ContentPlaceHolder1_DetailBar1_NewsTitle ').text(),
"fabushijain" :response.doc('span#ctl00_ContentPlaceHolder1_DetailBar1_ReleaseLabel').text(),
"content" : response.doc('#ctl00_ContentPlaceHolder1_DetailBar1_ContentInfo').text(),
}
def on_result(self, result):
#print result
if not result or not result['title']:
return
sql = SQL()
sql.replace('info',**result)
++++++++++++++++++++++++++++++++++++++++++++++
mysqldb.py 文件内容如下:
class SQL:
username = 'pyspider' #数据库用户名
password = 'pyspider' #数据库密码
database = 'result' #数据库
host = 'localhost' #数据库主机地址
connection = ''
connect = True
placeholder = '%s'
def __init__(self):
if self.connect:
SQL.connect(self)
def escape(self,string):
return '`%s`' % string
def connect(self):
config = {
'user':SQL.username,
'password':SQL.password,
'host':SQL.host
}
if SQL.database != None:
config['database'] = SQL.database
try:
cnx = mysql.connector.connect(**config)
SQL.connection = cnx
return True
except mysql.connector.Error as err:
if (err.errno == errorcode.ER_ACCESS_DENIED_ERROR):
print "The credentials you provided are not correct."
elif (err.errno == errorcode.ER_BAD_DB_ERROR):
print "The database you provided does not exist."
else:
print "Something went wrong: " , err
return False
def replace(self,tablename=None,**values):
if SQL.connection == '':
print "Please connect first"
return False
tablename = self.escape(tablename )
if values:
_keys = ", ".join(self.escape(k) for k in values)
_values = ", ".join([self.placeholder, ] * len(values))
sql_query = "REPLACE INTO %s (%s) VALUES (%s)" % (tablename, _keys, _values)
else:
、 sql_query = "REPLACE INTO %s DEFAULT VALUES" % tablename
cur = SQL.connection.cursor()
try:
if values:
cur.execute(sql_query, list(itervalues(values)))
else:
cur.execute(sql_query)
SQL.connection.commit()
return True
except mysql.connector.Error as err:
print ("An error occured: {}".format(err))
return False
有两个思路供参考:
检查一下包下边的__init__.py是否存在
检查一下模块名和导入的模块名是否一致,特别是这个模块被多个py文件导入
感谢支持 貌似不是这个原因
有可能是原来的mysql-python的连接版本过低导致,pip安装后是mysql-connector-python(1.0.11) Mysql-python(1.2.5)
参考版本地址: http://ftp.ntu.edu.tw/MySQL/Downloads/Connector-Python/
采用最新版的mysql-connector-python(2.1.2) 还是不行 继续测试中
版本是centos7的版本 已到 http://ftp.ntu.edu.tw/MySQL/Downloads/Connector-Python/下载的 2.1.2版本
是在什么平台上?如果是windows 需要到mysql的主页上找到对应的驱动包
ubuntu 上需要安装 apt-get install python-mysqldb 来安装这个包
pyspider 连接mysql_pyspider 导入模块报错 提示 No module named mysqldb相关推荐
- python3.7运行报错_使用python3.7运行报错:No module named 'MySQLdb'
以下是报错信息: Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs ...
- python no module named pandas_【原创】大叔经验分享(11)python引入模块报错ImportError: No module named pandas numpy...
python应用通常需要一些库,比如numpy.pandas等,安装也很简单,直接通过pip # pip install numpy Requirement already satisfied: nu ...
- python导入模块报错“ImportError: No module namedXXX”
用了python 有一段时间,发现基础的import都不熟悉,组里的同学跟我讲了下,我才大致明白,下面主要分两种情况说明: (1)编辑器(如Pycharm)导入时找不到模块 请确保你想要被引入的模块路 ...
- python3导入docx报错ImportError: No module named ‘exceptions‘
使用环境: python 3.5 原因: docx包中导入该模块,而python3.x版本移除exceptions模块.即docx包没有适配python3 解决办法: 1.使用管理员身份打开cmd,进 ...
- Python3环境下django连接mysql报错:No module named 'MySQLdb'
原因是python3中使用了一个新的模块,叫做pymysql,可以直接pip安装 pip安装完成后,在django项目的manage.py文件中添加下面两行就OK了: from django.core ...
- Python笔记-解决pyinstaller打包后运行时动态加载模块报错(No module named)
出现的问题就是:No module named 原因是我代码里面使用了这个 importlib.import_module pyinstaller没有检测到. 百度上没有找到解决办法,在外wang找到 ...
- python中自定义模块导入飘红_Pychorm提示Unresolved reference 导入模块报错
最近使用Pychorm编写Python时,每次要引入自定义模块,就会报错,提示"Unresolved reference" Unresolved reference 'LoginC ...
- PyCharm远程连接(linux)python解释器导入pyspark时报错:No module named ‘pyspark‘
PyCharm远程连接(linux)python解释器导入pyspark时报错:No module named 'pyspark' ,并且from pyspark import SparkConf, ...
- Python报错ModuleNotFoundError: No module named ‘pyqtgraph‘
在使用python对数据可视化需要 'pyqtgraph' 这个模块进行绘图,当我们在导入这个模块运行代码之后会有这样的报错:No module named 'pyqtgraph',这是因为当前使用的 ...
最新文章
- Windows中用运行命令直接启动指定软件的方法
- python 如何获取当前时间并转换为年月日
- node.js工程的结构
- linux搭建SonarQube_Oracle
- 起底 Windows 35 年发展史
- k3 lede刷官改_斐讯 K3 路由 LEDE 固件刷回官方原版固件
- 上传本地项目到码云仓库【图文详解】
- 电脑磁盘分区、合并的方法指南!
- win10电脑wifi显示无法连接服务器,Win10怎么连Wifi?解决Win10无法连接wifi无线网络的方法图文详解...
- 教你用 CSS 实现超真实的 3D 相册,让你的照片立体感 UPUP
- 苹果超薄触摸显示技术专利曝光:重新定义轻薄
- 如何使用Ajax更新echarts工作省份信息
- Epson机械手简单实例编程
- Serenity Screenplay模式
- java压缩解压缩rar、zip文件
- 老大让我看baidu他们的查公交是怎么做的,我就看了
- Java 变量作用域、构造方法官方教程
- Hannah荣获第六季完美童模全球总决赛全球人气总冠军
- 在Win7环境下搭建Geant4工作平台
- 软件架构与设计(四)-----数据流架构
热门文章
- centos 显示50g硬盘但是 实际大小很小_天啦噜!知道硬盘很慢,但没想到比 CPU Cache 慢 10000000 倍...
- eas库存状态调整单不能反审核_订单少了,库存多了,利润没了……
- 为什么转换不了html,特殊字符不会转换为html实体(html_entity_decode不工作)
- mysql 日志 超过 m,linux 下查找大日志文件,比如大于500M的文件
- python模拟键盘输入视频_python教程-模拟鼠标和键盘输入
- delphi socket 流的使用_Socket
- 基于ZYNQ实时目标检测系统
- 小ck活动机器人包包_别只知道小 CK!这 5 款小众包包更值得买
- python从txt拿取数据_python requests + xpath 获取分页详情页数据存入到txt文件中
- [Luogu1891]疯狂LCM[辗转相减法]