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

#!/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. Python import其他层级的模块

    [前言] Python的文件目录结构虽然层次清晰,结构清楚,但是在调用的时候可能还是出现各式各样的找不到路径的错误. [模块导入] 1.导入上一级目录的模块 python中导入上一级目录的模块有两种方 ...

  2. python3之模块

    1.python3模块 模块是一个包含所有你定义的函数和变量的文件,其后缀名是.py.模块可以被别的程序引入,以使用该模块中的函数等功能.这也是使用 python 标准库的方法. 模块让你能够有逻辑地 ...

  3. .net 下发送calendar

    前段时间公司系统中有一块需要发送邮件calendar outlook可以接受查看calendar 发送outlook主要是有rrule脚本的边界 网上找过一些资料,主要有两种实现方式 1.一种是已ic ...

  4. 七牛php-sdk使用-多媒体处理

    在七牛对象存储可以创建公共的bucket和私有的bucket,私有的不可以直接使用域名加资源key的方式进行访问,需要附加下载凭证. 私有bucket 关于下载凭证的生成,php-sdk已经提供了方法 ...

  5. MYBATIS常用的sql事例

    今天整理了一下在项目中经常用到的mybatis的xml文件的sql语句: 读者:有sql基础. <?xml version="1.0" encoding="UTF- ...

  6. LoadRunner 中实现MD5加密

    最近在用loadrunner做一个压力测试,在编写脚本的时候发现传递参数的时候需要一个sign值,这个值是将参数进行MD5加密生成的,所以下面就说一说怎么对参数进行MD5加密. 1.首先我们需要一个加 ...

  7. CSS3中first-child、last-child、nth-child、nth-last-child

    1.单独指定第一个子元素.最后一个子元素的样式 <style type="text/css"> li:first-child{ background:yellow; } ...

  8. 2017ecjtu-summer training #1 UVA 10399

    It has been said that a watch that is stopped keeps better time than one that loses 1 second per day ...

  9. JavaScript八张思维导图—编程风格

    JS基本概念 JS操作符 JS基本语句 JS数组用法 Date用法 JS字符串用法 JS编程风格 JS编程实践 不知不觉做前端已经五年多了,无论是从最初的jQuery还是现在火热的Angular,Vu ...

  10. 【Jsp/Servlet】获取客户端使用的ip

    一般使用jsp的时候大多数时间都可以使用request.getRemoteAddr() 来获取ip,但是这个前提是未经过反向代理等操作的原始地址,所以,需要在反向代理等操作之后还要获取客户端的ip变得 ...