pyspider 连接mysql_Pyspider 框架学习时走过的一些坑

背景:自己在做一个 V2EX 爬虫的时候,需要把爬取的帖子中的内容( title 和 content)保存在本地数据库。

环境:Pycharm 2016.1 + MySQL 5.7 + Pyspider + MySQL workbench + Python 2.7 32位

1. windows下安装MySQLdb出现的问题及其解决方法

你有两个选择:安装已编译好的版本(一分钟)

从官网下,自己编译安装(介个…..半小时到半天不等,取决于你的系统环境以及RP)若是系统32位的,有c++编译环境的,自认为RP不错的,可以选择自己编译安装,当然,遇到问题还是难免的,一步步搞还是能搞出来的

若是系统64位的,啥都木有的,建议下编译版本的,甭折腾安装已编译的版本

我的电脑,win7, 64位,python 2.7 32位版本

所以选择 MySQL-python-1.2.3.win32-py2.7.exe

自己编译安装:安装MySQLdb

下载MySQLdb MySQL for Python download

解压后,cmd进入对应文件夹,如果32位系统且有gcc编译环境,直接 python setup.py build

问题汇总

1. 64位系统,无法读取注册表的问题

异常信息如下:F:\devtools\MySQL-python-1.2.3>pythonsetup.py build

Traceback (most recent call last):

File “setup.py”, line 15, in

metadata, options = get_config()

File “F:\devtools\MySQL-python-1.2.3\setup_windows.py”, line7, in get_config

serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, options[’ registry_ke

y’] )

WindowsError: [Error 2] The system cannotfind the file specified

解决方法:其实分析代码,发现只是寻找mysql的安装地址而已 修改setup_windows.py如下

注解两行,加入一行,为第一步mysql的安装位置

serverKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,options[‘registry_key’] )

mysql_root, dummy = _winreg.QueryValueEx(serverKey,’Location’)

mysql_root = r”F:\devtools\MySQL\MySQL Server 5.5”

2. 没有gcc编译环境unable to find vcvarsall.bat

解决方法:安装编译环境(一个老外的帖子)1) First ofall download MinGW. Youneed g++compiler and MingW make in setup.

2) If youinstalled MinGW for example to “C:\MinGW” then add “C:\MinGW\bin”to your PATH in Windows.(安装路径加入环境变量)

3) Now startyour Command Prompt and Go the directory where you have your setup.py residing.

4) Last andmost important step:

setup.py install build –compiler=mingw32

或者在setup.cfg中加入:

[build]

compiler = mingw32

3. gcc: /Zl: No suchfile or directory错误

异常信息如下F:\devtools\MinGW\bin\gcc.exe -mno-cygwin-mdll -O -Wall -Dversion_info=(1,2,3,’

final’,0) -D__version__=1.2.3”-IF:\devtools\MySQL\MySQL Server 5.5\include” -IC

:\Python27\include -IC:\Python27\PC -c_mysql.c -o build\temp.win-amd64-2.7\Rele

ase_mysql.o /Zl

gcc: error: /Zl: No such file or directory

error: command ‘gcc’ failed with exitstatus 1

参数是vc特有的编译参数,如果使用mingw的话因为是gcc所以不支持。可以在setup_windows.py中去掉

/Zl

解决方法:修改setup_windows.py 改为空的

extra_compile_args = [ ‘/Zl’ ]

extra_compile_args = [ ” ]

测试有没有安装成功?在cmd命令行下执行如下图命令:

如果没有报错的话,则表示成功了。

代码测试(首先得创建数据库表)

DROP TABLE IF EXISTS `question`;

CREATE TABLE `question` (

`id` INT NOT NULL AUTO_INCREMENT,

`title` VARCHAR(255) NOT NULL,

`content` TEXT NULL,

`user_id` INT NOT NULL,

`created_date` DATETIME NOT NULL,

`comment_count` INT NOT NULL,

PRIMARY KEY (`id`),

INDEX `date_index` (`created_date` ASC));

python 代码:

#-*-coding:utf8-*-

#created by 10412

import MySQLdb

import random

#测试连接数据库

if __name__ == '__main__':

db = MySQLdb.connect("localhost", "root", "root", "wenda", charset="utf8")

try:

cursor = db.cursor()

#插入数据

sql = 'insert into question(title, content, user_id, created_date, comment_count) values("怎样能够写出一篇10万+的文章出来?", "看到微信公众号好多大V写出的文章阅读量都是这么多,但是不知道自己可以怎么写,需要注意点什么吗?", 14, now(), 0)'

cursor.execute(sql)

qid = cursor.lastrowid

db.commit()

print qid

#查询数据

sql = 'select * from question order by id desc limit 3'

cursor.execute(sql)

for each in cursor.fetchall():

for row in each:

print row

db.commit()

except Exception, e:

print e

db.rollback()

db.close()

运行结果如下:

就表明成功了。

2. HTTP 599: SSL certificate problem: unable to get local issuer certificate错误

解决方法:

使用 self.crawl(url, callback=self.index_page, validate_cert=False)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值