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. docker 无法使用vi

    更新来源 apt-get update   安装vim apt-get install -y vim

  2. H5存储方式

    数据存储 var arr = [0, 1, 1, 1]; //存储,IE6~7 cookie 其他浏览器HTML5本地存储 if (window.localStorage) { localStorag ...

  3. Jboss未授权访问部署木马 利用exp

    查看系统名称 java -jar jboss_exploit_fat.jar -i http://www.any.com:8080/invoker/JMXInvokerServlet get jbos ...

  4. ajax 传参数 java后台接收

  5. 如何成为优秀的技术Leader

    技术主管,又叫技术经理,英文一般是 Tech Leader ,简称 TL.随着工作经验的不断积累,能力的不断提升,每个人都有机会成为 Team Leader. 然而在机会到来前,我们必须提前做好准备, ...

  6. Maven 基础概念

    Project:任何你想构建的事务Maven都可以认为它们是工程,这些工程被定义为工程对象模型(project Object Model POM) 一个工程可以依赖其他的工程,一个工程也可以由多个子工 ...

  7. 05_Hive分区总结

    2.1.创建分区表并将本地文件的数据加载到分区表: 使用下面的命令来创建一个带分区的表 通过partitioned by(country string)关键字声明该表是分区表,且分区字段不能为crea ...

  8. vue-element-admin后台的安装

    # 克隆项目 git clone https://github.com/PanJiaChen/vue-element-admin.git # 进入项目目录 cd vue-element-admin # ...

  9. PAT Advanced 1153 Decode Registration Card of PAT (25 分)

    A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, nam ...

  10. jQuery.fn.extend() 函数详解

    jQuery.fn.extend()函数用于为jQuery扩展一个或多个实例属性和方法(主要用于扩展方法). jQuery.fn是jQuery的原型对象,其extend()方法用于为jQuery的原型 ...