获取字段的存储处理和获取普通的路径

#!/usr/bin/python3
# -*- coding: utf-8 -*- import pymysql
import gevent
import pymysql
from gevent import monkey
from scrapy.pipelines.images import ImagesPipeline
import pymysql.cursors class JobboleImagerPipeline(ImagesPipeline):
"""
获得图片下载路径
"""
def item_completed(self, results, item, info):
if 'img_url' in item:
for key, value in results:
# print(key)
img_path = value['path']
# print(value['path'])
item['img_path'] = img_path
return item # class SqlSave(object):
# """常规同步方式存入数据库"""
# def __init__(self):
# SQL_DBA = {
# 'host': 'localhost',
# 'db': 'jobole',
# 'user': 'root',
# 'password': 'password',
# 'use_unicode': True,
# 'charset': 'utf8'
# }
# self.conn = pymysql.connect(**SQL_DBA)
# self.cursor = self.conn.cursor()
#
# def process_item(self, item, spider):
# sql = self.get_sql(item)
# print(sql)
# self.cursor.execute(sql)
# self.conn.commit()
#
# return item
#
# def get_sql(self, item):
# sql = """insert into article(cont_id, cont_url, title, publish_time, cont, img_url, img_path, like_num, collection_num, comment_num) value ('%s','%s','%s','%s','%s','%s','%s', %d, %d, %d)
# """ % (item['cont_id'], item['cont_url'],item['title'],item['publish_time'],item['cont'],item['img_url'][0],item['img_path'],item['link_num'],item['collection_num'],item['comment_num'],)
# return sql class SqlSave(object):
"""
协程方式向数据库插入数据
""" def __init__(self):
# 初始数据库连接和参数,SQL_DBA可写在setting中,通过 获取在settings.py中设置的SQL_DBA字典
# @classmethod
# def from_settings(cls, settings):
# sql_dba = settings[SQL_DBA]
# return cls(cls,sql_dba) 需要__init__中新添个参数接收这个值
SQL_DBA = {
'host': 'localhost',
'db': 'jobole',
'user': 'root',
'password': 'password',
'use_unicode': True,
'charset': 'utf8'
}
self.conn = pymysql.connect(**SQL_DBA)
self.cursor = self.conn.cursor() def process_item(self, item, spider):
sql = self.__get_sql(item)
# 协程方式对数据库插入操作
gevent.joinall([
gevent.spawn(self.__go_sql, self.cursor, self.conn, sql, item),
])
return item def __go_sql(self, cursor, conn, sql, item):
try:
# 数据库插入操作
cursor.execute(sql,
(item['cont_id'], item['cont_url'], item['title'], item['publish_time'],
item['cont'], item['img_url'][0], item['img_path'], item['link_num'],
item['collection_num'], item['comment_num']))
conn.commit()
except Exception as e:
print(e) def __get_sql(self, item):
# 生成sql语句
sql = """insert into
article(cont_id, cont_url, title, publish_time,
cont, img_url, img_path, like_num,
collection_num, comment_num)
value
(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"""
return sql

  

爬取知名社区技术文章_pipelines_4的更多相关文章

  1. 爬取知名社区技术文章_items_2

    item中定义获取的字段和原始数据进行处理并合法化数据 #!/usr/bin/python3 # -*- coding: utf-8 -*- import scrapy import hashlib ...

  2. 爬取知名社区技术文章_setting_5

    # -*- coding: utf-8 -*- # Scrapy settings for JobBole project # # For simplicity, this file contains ...

  3. 爬取知名社区技术文章_article_3

    爬虫主逻辑处理,获取字段,获取主url和子url #!/usr/bin/python3 # -*- coding: utf-8 -*- import scrapy from scrapy.http i ...

  4. 第4章 scrapy爬取知名技术文章网站(2)

    4-8~9 编写spider爬取jobbole的所有文章 # -*- coding: utf-8 -*- import re import scrapy import datetime from sc ...

  5. 爬取博主所有文章并保存到本地(.txt版)--python3.6

    闲话: 一位前辈告诉我大学期间要好好维护自己的博客,在博客园发布很好,但是自己最好也保留一个备份. 正好最近在学习python,刚刚从py2转到py3,还有点不是很习惯,正想着多练习,于是萌生了这个想 ...

  6. 爬虫实战——Scrapy爬取伯乐在线所有文章

    Scrapy简单介绍及爬取伯乐在线所有文章 一.简说安装相关环境及依赖包 1.安装Python(2或3都行,我这里用的是3) 2.虚拟环境搭建: 依赖包:virtualenv,virtualenvwr ...

  7. Node爬取简书首页文章

    Node爬取简书首页文章 博主刚学node,打算写个爬虫练练手,这次的爬虫目标是简书的首页文章 流程分析 使用superagent发送http请求到服务端,获取HTML文本 用cheerio解析获得的 ...

  8. 使用Python爬取微信公众号文章并保存为PDF文件(解决图片不显示的问题)

    前言 第一次写博客,主要内容是爬取微信公众号的文章,将文章以PDF格式保存在本地. 爬取微信公众号文章(使用wechatsogou) 1.安装 pip install wechatsogou --up ...

  9. Python3.6+Scrapy爬取知名技术文章网站

    爬取分析 伯乐在线已经提供了所有文章的接口,还有下一页的接口,所有我们可以直接爬取一页,再翻页爬. 环境搭建 Windows下安装Python: http://www.cnblogs.com/0bug ...

随机推荐

  1. docker:(4)利用WebHook实现持续集成

    研发小伙伴可能对下列操作步骤会深有体会 写代码-->提交代码-->打包-->发布 在项目调试测试阶段,可能经常需要重复上面的步骤,以便将最新代码部署到特定环境供测试人员或其他人员使用 ...

  2. linux 卸载安装node npm

    1. 卸载node npm (1) 先卸载 npm: sudo npm uninstall npm -g (2) 然后卸载 Node.js. (2.1) 如果是 Ubuntu 系统并使用 apt-ge ...

  3. 用JAVA写一个冒泡排序

    一:实现思想: 基本思想:在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒.即:每当两相邻的数比较后发现它们的排序与排序要 ...

  4. Flask中的单例模式

    1,基于文件的单例模式: import pymysql import threading from DBUtils.PooledDB import PooledDB class SingletonDB ...

  5. Linux-RED HAT6.8扩容

    项目快上线了,在准生产测试,遇到了一个问题,那就是Linux硬盘资源不够用了,这个问题是当时在装系统的时候,应该装机人员的失误造成的,具体如下: 这个报错原因就是硬盘资源不够用了,使用命令:df  - ...

  6. 声音变调算法PitchShift(模拟汤姆猫) 附完整C++算法实现代码

    上周看到一个变调算法,挺有意思的,原本计划尝试用来润色TTS合成效果的. 实测感觉还需要进一步改进,待有空再思考改进方案. 算法细节原文,移步链接: http://blogs.zynaptiq.com ...

  7. MyEclipse10.7 10.6导出war文件报错 “SECURITY ALERT: INTEGERITY CHECK ERROR”

    myeclipse10.7的版本破解后,导出war包时报“SECURITY ALERT: INTEGERITY CHECK ERROR”的错误. 经过测试本次破解可以解决此错误,只需替换一个jar文件 ...

  8. Gym 101612C Consonant Fencity

    原题传送门 题意:给定一个只含小写字母的字符串,假设aeiouyw为元音字母,现在要把一些字母变为大写,要求相同字母的大小写必须相同,统计变化后的字符串中有多少对相邻.同为辅音字母且大小写不一样的字符 ...

  9. python写zip破解器

    浏览桌面依然平静,!!!!等等..怎么有个压缩包 打开一看!!!156.txt???waht the fuck? 卧槽还有密码!!!!!! 但是我不知道╮(╯▽╰)╭该怎么办呢! 很简单,python ...

  10. hdu_2669 Romantic(扩展欧几里得)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2669 Romantic Time Limit: 2000/1000 MS (Java/Others)  ...