python能不能爬数据库_python爬取数据后不能写入到数据库中

目标:去爬当当网某页内容中的书籍的名称,链接和评论数,然后写入数据库中

pipelins内容如下:

import pymysql

class DangdangPipeline(object):def process_item(self, item, spider):

conn = pymysql.connect(host="127.0.0.1",port=3306, user="root", passwd="", db="dd")#连接数据库

cur = conn.cursor()#创建游标对象

for i in range(0,len(item["title"])):

title=item["title"][i]

link=item["link"][i]

comment=item["comment"][i]

sql="insert into books(title,link,comment) values('title','link','comment')"#构造sql语句;插入值

conn.commit()#提交事务

cur.execute(sql)#执行sql

conn.close()#关闭数据库连接状态

return item

爬虫文件就不贴了,因为单独运行爬虫,注释掉写入数据库的步骤后运行是没有问题的,就是写入数据库中出了问题;

另外,我的数据库里面已经建好了名为dd的databese,而且建立了名为books的table如下:mysql> select * from books;titlelinkcomment

123

titlelink0

title1link10

3 rows in set (0.00 sec)

里面的一些内容是我之前在python的shell模式下测试能否连接和写入时写入的;命令和执行结果如下:import pymysql

conn=pymysql.connect(host="127.0.0.1",user="root",password="",database="dd")

cur=conn.cursor()

cur.execute("insert into books(title,link,comment) values('1','2','3')")

1

conn.commit()

cur.execute("select * from books")

2

cur.fetchall()

(('1', '2', 3), ('title1', 'link1', 0))

conn.close()

我在pipelines里面使用的连接和写入数据库的语句都是用得之前在python的shell模式下运行成功的语句,为啥不行呢,

下面是在cmd中运行中出现的错误:

2018-05-02 12:48:05 [scrapy.utils.log] INFO: Scrapy 1.5.0 started (bot: dangdang)

2018-05-02 12:48:05 [scrapy.utils.log] INFO: Versions: lxml 4.2.1.0, libxml2 2.9.7, cssselect 1.0.3, parsel 1.4.0, w3lib 1.19.0, Twisted 17.9.0, Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)], pyOpenSSL 17.5.0 (OpenSSL 1.1.0h 27 Mar 2018), cryptography 2.2.2, Platform Windows-10-10.0.16299-SP0

2018-05-02 12:48:05 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME': 'dangdang', 'NEWSPIDER_MODULE': 'dangdang.spiders', 'SPIDER_MODULES': ['dangdang.spiders']}

2018-05-02 12:48:05 [scrapy.middleware] INFO: Enabled extensions:

['scrapy.extensions.telnet.TelnetConsole',

'scrapy.extensions.logstats.LogStats',

'scrapy.extensions.corestats.CoreStats']

2018-05-02 12:48:05 [scrapy.middleware] INFO: Enabled downloader middlewares:

['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',

'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',

'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',

'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',

'scrapy.downloadermiddlewares.retry.RetryMiddleware',

'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',

'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',

'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',

'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',

'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',

'scrapy.downloadermiddlewares.stats.DownloaderStats']

2018-05-02 12:48:06 [scrapy.middleware] INFO: Enabled spider middlewares:

['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',

'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',

'scrapy.spidermiddlewares.referer.RefererMiddleware',

'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',

'scrapy.spidermiddlewares.depth.DepthMiddleware']

2018-05-02 12:48:06 [scrapy.middleware] INFO: Enabled item pipelines:

['dangdang.pipelines.DangdangPipeline']

2018-05-02 12:48:06 [scrapy.core.engine] INFO: Spider opened

2018-05-02 12:48:06 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)

2018-05-02 12:48:06 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023

2018-05-02 12:48:06 [scrapy.core.engine] DEBUG: Crawled (200) (referer: None)

2018-05-02 12:48:06 [scrapy.core.scraper] DEBUG: Scraped from <200 http://www.dangdang.com/>

{'comment': [], 'link': [], 'title': []}

2018-05-02 12:48:06 [scrapy.core.engine] DEBUG: Crawled (200) (referer: http://www.dangdang.com/)

2018-05-02 12:48:07 [scrapy.core.scraper] ERROR: Error processing {'comment': ['5470条评论','120条评论',

'6257条评论',

'7448条评论',

'1275条评论',

'7518条评论',

'25163条评论',

'10043条评论',

'6621条评论',

'8796条评论',

'8546条评论',

'22721条评论',

'15684条评论',

'4726条评论',

'677条评论',

'1829条评论',

'5966条评论',

'1506条评论',

'7959条评论',

'14494条评论',

'339条评论',

'21条评论',

'86条评论',

'4208条评论',

'14399条评论',

'1951条评论',

'853条评论',

'4585条评论',

'3559条评论',

'7243条评论',

'465条评论',

'2832条评论',

'9721条评论',

'217条评论',

'5659条评论',

'4279条评论',

'2977条评论',

'10422条评论',

'26条评论',

'1086条评论',

'4526条评论',

'18232条评论',

'139条评论',

'217条评论',

'12190条评论',

'325条评论',

'1579条评论',

'743条评论',

'4089条评论',

'400条评论',

'9条评论',

'3024条评论',

'1448条评论',

'2273条评论',

'14271条评论',

'250条评论',

'3255条评论',

'45条评论',

'845条评论',

'4288条评论'],'http://product.dangdang.com/25218035.html',

'http://product.dangdang.com/22880871.html',

'http://product.dangdang.com/23961748.html',

'http://product.dangdang.com/23997502.html',

'http://product.dangdang.com/24042210.html',

'http://product.dangdang.com/22783504.html',

'http://product.dangdang.com/23259731.html',

'http://product.dangdang.com/23958142.html',

'http://product.dangdang.com/23254747.html',

'http://product.dangdang.com/23368089.html',

'http://product.dangdang.com/9265169.html',

'http://product.dangdang.com/22628333.html',

'http://product.dangdang.com/24035306.html',

'http://product.dangdang.com/25108303.html',

'http://product.dangdang.com/23617284.html',

'http://product.dangdang.com/23734636.html',

'http://product.dangdang.com/23928893.html',

'http://product.dangdang.com/22606835.html',

'http://product.dangdang.com/22722790.html',

'http://product.dangdang.com/25071204.html',

'http://product.dangdang.com/25227022.html',

'http://product.dangdang.com/25159352.html',

'http://product.dangdang.com/23594855.html',

'http://product.dangdang.com/23321562.html',

'http://product.dangdang.com/23926195.html',

'http://product.dangdang.com/23954702.html',

'http://product.dangdang.com/23931418.html',

'http://product.dangdang.com/23794057.html',

'http://product.dangdang.com/23532609.html',

'http://product.dangdang.com/25111311.html',

'http://product.dangdang.com/24142436.html',

'http://product.dangdang.com/20459091.html',

'http://product.dangdang.com/25107162.html',

'http://product.dangdang.com/24144166.html',

'http://product.dangdang.com/23964002.html',

'http://product.dangdang.com/23918741.html',

'http://product.dangdang.com/20165942.html',

'http://product.dangdang.com/25218232.html',

'http://product.dangdang.com/23834026.html',

'http://product.dangdang.com/21063086.html',

'http://product.dangdang.com/23473514.html',

'http://product.dangdang.com/25163118.html',

'http://product.dangdang.com/25089827.html',

'http://product.dangdang.com/20255354.html',

'http://product.dangdang.com/25086349.html',

'http://product.dangdang.com/24188655.html',

'http://product.dangdang.com/24214704.html',

'http://product.dangdang.com/23371791.html',

'http://product.dangdang.com/25071121.html',

'http://product.dangdang.com/25249602.html',

'http://product.dangdang.com/23462067.html',

'http://product.dangdang.com/24159761.html',

'http://product.dangdang.com/24011143.html',

'http://product.dangdang.com/21110580.html',

'http://product.dangdang.com/25123327.html',

'http://product.dangdang.com/23800641.html',

'http://product.dangdang.com/25195904.html',

'http://product.dangdang.com/25060860.html',

'http://product.dangdang.com/20569834.html'],

'title': [' Python编程 从入门到实践',' Python基础教程(第3版)',

' 算法(第4版)【Sedgewick之巨著,与高德纳TAOCP一脉相承】',

' Python核心编程 第3版',

' Python编程快速上手 让繁琐工作自动化(Python3编程从入门到实践 新手学习必备用书)',

' Java从入门到精通(第4版)(附光盘)',

' C++ Primer Plus(第6版)中文版(畅销30年C++必读经典教程全新升级,蔡学镛、孟岩、高博倾力推荐)',

' 深入理解Java虚拟机:JVM高级特性与最佳实践(第2版)',

' C Primer Plus 第6版 中文版 C语言入门经典教程',

' 机器学习实战【python基础教程指南,python核心编程实例指导,对wxpython数据库充分的讲解,不可错过的编',

' 利用Python进行数据分析',

' Head First Java(中文版)(JAVA经典畅销书 生动有趣 轻松学好JAVA)',

' JavaScript高级程序设计(第3版)(JavaScript技术名著,html+css+javascript教程精',

' Java核心技术 卷I:基础知识(原书第10版)',

' HTML5+CSS3+JavaScript从入门到精通(标准版) web开发技术基础视频讲解与案例实战,8大素材库、源',

' Python编程(第四版)',

' 重构 改善既有代码的设计',

' 教孩子学编程 Python语言版',

' Java并发编程实战(第16届Jolt大奖提名图书,Java并发编程必读佳作)',

' JavaScript权威指南(第6版)(淘宝前端团队倾情翻译!经典权威的JavaScript犀牛书!第6版特别涵盖了H',

' 微信小程序开发图解案例教程(附精讲视频)',

' 精通Python爬虫框架Scrapy',

' 区块链原理、设计与应用',

' “笨办法”学Python(第3版)',

' C++ Primer中文版(第5版)',

' JavaEE开发的颠覆者: Spring Boot实战',

' 程序员的数学1+2+3 数学思维+概率统计+线性代数(套装共3册)',

' Spring实战(第4版)',

' 动手玩转Scratch2.0编程—STEAM创新教育指南',

' 疯狂Java讲义(第4版)',

' Java EE互联网轻量级框架整合开发— —SSM框架(Spring MVC+Spring+MyBatis)和Redi',

' MATLAB R2016a完全自学一本通',

' Effective Java中文版(第2版)',

' 区块链核心算法解析',

' 第一行代码 Android 第2版',

' R语言实战 第2版',

' 数据结构与算法分析:Java语言描述(原书第3版)',

' C 和指针(C指针的经典之作,读者好评如潮)',

' Python程序设计 第3版',

' Python算法教程',

' Python学习手册:第4版(涵盖Python2.6和3.X)',

' Python基础教程(第2版·修订版)',

' Java从入门到精通(项目案例版)扫码看视频重印80次销售50万册',

' Android Studio开发实战:从零基础到App上线',

' Python核心编程(第二版)(Python领域经典作品、非常有名的开发指南)',

' PHP从入门到精通(第4版)',

' C语言从入门到精通(第3版)',

' 算法图解',

' 深入浅出Node.js【深入讲解Node的图书,详细讲述基于JavaScript运行时所建立的平台原理,与mongod',

' 流畅的Python',

' Python 3网络爬虫开发实战',

' 图解HTTP',

' Python机器学习 预测分析核心算法',

' C#入门经典第7版',

' 用户体验要素:以用户为中心的产品设计(原书第2版)(决战大数据时代!IT技术人员不得不读!)',

' Python 3.5从零开始学',

' Redis实战',

' 数据结构 Python语言描述',

' Scratch少儿趣味编程2',

' 程序员的自我修养—链接、装载与库( 网易云风力荐:莫到用时再读书!)']}

Traceback (most recent call last):

File "d:\python3.5\lib\site-packages\twisted\internet\defer.py", line 653, in _runCallbackscurrent.result = callback(current.result, *args, **kw)

File "E:\python\第7次课\dangdang\dangdang\pipelines.py", line 20, in process_itemcur.execute(sql)#执行sql

File "d:\python3.5\lib\site-packages\pymysql\cursors.py", line 165, in executeresult = self._query(query)

File "d:\python3.5\lib\site-packages\pymysql\cursors.py", line 321, in _queryconn.query(q)

File "d:\python3.5\lib\site-packages\pymysql\connections.py", line 860, in queryself._affected_rows = self._read_query_result(unbuffered=unbuffered)

File "d:\python3.5\lib\site-packages\pymysql\connections.py", line 1061, in _read_query_resultresult.read()

File "d:\python3.5\lib\site-packages\pymysql\connections.py", line 1349, in readfirst_packet = self.connection._read_packet()

File "d:\python3.5\lib\site-packages\pymysql\connections.py", line 1018, in _read_packetpacket.check_error()

File "d:\python3.5\lib\site-packages\pymysql\connections.py", line 384, in check_errorerr.raise_mysql_exception(self._data)

File "d:\python3.5\lib\site-packages\pymysql\err.py", line 107, in raise_mysql_exceptionraise errorclass(errno, errval)

pymysql.err.IntegrityError: (1062, "Duplicate entry 'title' for key 'PRIMARY'")

2018-05-02 12:48:07 [scrapy.dupefilters] DEBUG: Filtered duplicate request: - no more duplicates will be shown (see DUPEFILTER_DEBUG to show all duplicates)

2018-05-02 12:48:07 [scrapy.core.engine] INFO: Closing spider (finished)

2018-05-02 12:48:07 [scrapy.statscollectors] INFO: Dumping Scrapy stats:

{'downloader/request_bytes': 498,

'downloader/request_count': 2,

'downloader/request_method_count/GET': 2,

'downloader/response_bytes': 72998,

'downloader/response_count': 2,

'downloader/response_status_count/200': 2,

'dupefilter/filtered': 1,

'finish_reason': 'finished',

'finish_time': datetime.datetime(2018, 5, 2, 4, 48, 7, 274283),

'item_scraped_count': 1,

'log_count/DEBUG': 5,

'log_count/ERROR': 1,

'log_count/INFO': 7,

'request_depth_max': 2,

'response_received_count': 2,

'scheduler/dequeued': 2,

'scheduler/dequeued/memory': 2,

'scheduler/enqueued': 2,

'scheduler/enqueued/memory': 2,

'start_time': datetime.datetime(2018, 5, 2, 4, 48, 6, 44732)}

2018-05-02 12:48:07 [scrapy.core.engine] INFO: Spider closed (finished)

完全自学,还请各位老师指点,另外我之前学c语言的时候,我记得执行都是按顺序执行的,为啥这里先爬取comment呢?

我的程序里它是在最后啊,title是在前面的啊?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值