import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule from wxapp.items import WxappItem class WxSpider(CrawlSpider):
name = 'wx'
allowed_domains = ['wxapp-union.com']
start_urls = ['http://www.wxapp-union.com/portal.php?mod=list&catid=2&page=1'] rules = (
Rule(LinkExtractor(allow=r'.*mod=list&catid=2&page=\d+'), follow=True),
Rule(LinkExtractor(allow=r'.*article-.+\.html'), callback='parse_detail', follow=False),
) def parse_detail(self, response):
detail_href = response.request.url
title = response.xpath('//h1[@class="ph"]/text()').get()
content = response.xpath('//td[@id="article_content"]//text()').getall()
content = [c.strip() for c in content]
content = ''.join(content).strip()
pub_time = response.xpath('//p[@class="authors"]/span/text()').get()
author = response.xpath('//p[@class="authors"]/a/text()').get()
item = WxappItem(title=title, content=content, detail_href=detail_href, pub_time=pub_time, author=author)
yield item

items:

class WxAppItem(scrapy.Item):
title = scrapy.Field()
pub_time = scrapy.Field()
content = scrapy.Field()
summary = scrapy.Field()
article_url = scrapy.Field()
read_count = scrapy.Field()

pipline:

import pymysql
from pymysql import cursors
from twisted.enterprise import adbapi class WxAppPipeline(object):
def __init__(self):
db_params = {
'host': '127.0.0.1',
'port': 3306,
'user': 'root',
'password': '',
'database': 'wxapp',
'charset': 'utf8',
'cursorclass': cursors.DictCursor # 指定游标类
}
# 定义数据库连接池
self.db_pool = adbapi.ConnectionPool('pymysql', **db_params)
self._sql = None def process_item(self, item, spider):
defer = self.db_pool.runInteraction(self.insert_item, item)
defer.addErrback(self.handle_error, item, spider)
return item def insert_item(self, cursor, item):
print('kkkkkkkkkkkkkkkkkkkk')
cursor.execute(self.sql, (item['title'], item['content'], item['summary'], item['read_count'], item['pub_time'], item['article_url'])) def handle_error(self, error, item, spider):
print('=' * 10 + 'error' + '=' * 10)
print(error) @property
def sql(self):
if not self._sql:
self._sql = """
INSERT INTO article(id, title, content, summary, read_count, pub_time, article_url) VALUES (null, %s, %s, %s, %s, %s, %s);
"""
return self._sql
return self._sql

scarpy crawl 爬取微信小程序文章(将数据通过异步的方式保存的数据库中)的更多相关文章

  1. scarpy crawl 爬取微信小程序文章

    import scrapy from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider ...

  2. python爬取微信小程序(实战篇)

    python爬取微信小程序(实战篇) 本文链接:https://blog.csdn.net/HeyShHeyou/article/details/90452656 展开 一.背景介绍 近期有需求需要抓 ...

  3. Python爬取微信小程序(Charles)

    Python爬取微信小程序(Charles) 本文链接:https://blog.csdn.net/HeyShHeyou/article/details/90045204 一.前言 最近需要获取微信小 ...

  4. scrapy爬取微信小程序社区教程(crawlspider)

    爬取的目标网站是: http://www.wxapp-union.com/portal.php?mod=list&catid=2&page=1 目的是爬取每一个教程的标题,作者,时间和 ...

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

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

  6. 微信小程序的ajax数据请求wx.request

    微信小程序的ajax数据请求,很多同学找不到api在哪个位置,这里单独把小程序的ajax请求给列出来,微信小程序的请求就是wx.request这个api,wx.request(一些对象参数),微信小程 ...

  7. 《吐血整理》高级系列教程-吃透Fiddler抓包教程(34)-Fiddler如何抓取微信小程序的包-上篇

    1.简介 有些小伙伴或者是童鞋们说小程序抓不到包,该怎么办了???其实苹果手机如果按照宏哥前边的抓取APP包的设置方式设置好了,应该可以轻松就抓到包了.那么安卓手机小程序就比较困难,不是那么友好了.所 ...

  8. 如何抓取微信小程序的源码?

    一.引言: 在工作中我们会想把别人的代码直接拿过来进行参考,当然这个更多的是前端代码的进行获取. 那么微信小程序的代码怎么样获取呢?  参考 https://blog.csdn.net/qq_4113 ...

  9. 微信小程序文章收录

    基础篇 03-04 微信登入小程序与后端实现 - 小猿取经 - 博客园 我做的小程序 - 小y - 博客园 小程序二维码和小程序带参数二维码生成 - Likwo - 博客园 accesstoken 微 ...

随机推荐

  1. Oracle 以及 PLSQL安装

    今天重装系统遇到oracle 安装的问题咯 ,oracle安装过程中很多疑难杂症咯 1如果之前装过,记得去删除注册表的Oracle 相关的文件 ,请百度有很多教程咯 2这个必须要勾选的!因为的是11g ...

  2. windows下Jmeter压测端口占用问题(亲测有效)

    windows下Jmeter压测端口占用问题 1 报错信息描述 压测的初期,在设置了 150qps/s 的并发数下压测几分钟后 Jmeter 就出现了如下报错. JAVA.NET.BINDEXCEPT ...

  3. JS的一些简单基础运算题

    1.输入一个四位数,在控制台分别显示个位,十位,百位,千位的数值 var a = prompt("请输入一个四位数的正整数"); var b = parseInt(a/1000); ...

  4. Category VS Extension 原理详解

    (一)Category 1.什么是Category? category是Objective-C 2.0之后添加的语言特性,别人口中的分类.类别其实都是指的category.category的主要作用是 ...

  5. 7.Spring整合Hibernate_1

    Spring 整合 Hibernate 1.Spring指定 database,给下面创建的 SessionFactory用 <!-- !!!!!可以使用 @Resource 将 这个bean对 ...

  6. SpringMVC【二、项目搭建】

    HelloWorld搭建 1.用Maven WebApp框架创建一个项目 红框中的是后添加的 2.添加pom引用(此处因为要引用多个spring包,建议把版本号提出来放到Properties) 会导入 ...

  7. 第二章、Django以及数据库的配置

    目录 第二章.Django以及数据库的配置 一.小白必会三板斧 二.静态文件配置 三.form表单 action和method参数可以写的形式 四.request对象及方法 五.django连接数据库 ...

  8. python遍历目录下所有文件

    # -*- coding:utf-8 -*- import os if __name__ == "__main__": rootdir = '.\data' list = os.l ...

  9. Kinect for windows的脸部识别

    需要引入的dll: 需要将下面两个dll复制到当前路径 Kinect for windows提供了脸部识别的功能,可以识出人脸.主要是通过FaceTrackFrame类的GetTriangles()来 ...

  10. JS 时间差计算 XX秒前、XX小时前、XX天前

    //时间差 function GetTime(time) {//di作为一个变量传进来 //如果时间格式是正确的,那下面这一步转化时间格式就可以不用了 var dateBegin = new Date ...