第一篇文章,写在我初步完成scrapy的项目。
在接触scrapy之前,首先学习python,做过几道爬虫挑战题目,做到第四题,感觉有点难度。
所以首先,写几个新手常见问题。
1、python库找不到,怎么scrapy.item就是没找到啊,怎么回事?
python查找库的路径有问题,最简单的解决方式,我喜欢最简单直接粗暴的方式,来解决问题。
不是import sys各位。千万别这样做,那会永无止境的添加sys.path.append了。
python27文件夹下..\lib\site-package知道吧,里面看一看,有没有很多.pth文件。你建立一个,写上d:\myprograme即可,以后编程序,都在这文件夹里编写,即可,一劳永逸。
2、python 连接 mysql 怎么一直失败啊,怎么数据插入报错啊,什么"Can't connect to MySQL server on 'localhost' (10061)" 什么AttributeError: 'function' object has no attribute 'cursor'。
你连接数据库的代码有问题,python对于localhost解析上有毛病,不能用localhost,而要用127.0.0.1来做,数据的对应也要注意和程序定义的表名字,关键字名字一致即可。
class MySQLStorePipeline(object):
def __init__(self):
self.conn = MySQLdb.connect("127.0.0.1", "root", "root", "test", charset="utf8", use_unicode=True)
self.cursor = self.conn.cursor()
def process_item(self, item, spider):
try:
self.cursor.execute("""INSERT INTO spider_items (area, salary, link)
VALUES (%s, %s, %s)""",
(item['area'][0].encode('utf-8'),
item['salary'][0].encode('utf-8'),
item['link'].encode('utf-8')))
self.conn.commit()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
return item
3、nosql的mongodb连接时候报错,mongoGUI怎么连接数据库,就是不出来,答案就是--auth惹的祸。
mongod.exe --logpath d:/websoft/mongodb/logs --logappend --dbpath d:/websoft/mongodb/data --directoryperdb --serviceName MongoDB -install 如果你加上了 -auth就导致链接GUI失败,大家切记。
如果加了怎么办,请到注册表中删除--auth即可。