scarpy crawl 爬取微信小程序文章(将数据通过异步的方式保存的数据库中)
- 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 爬取微信小程序文章(将数据通过异步的方式保存的数据库中)的更多相关文章
- scarpy crawl 爬取微信小程序文章
import scrapy from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider ...
- python爬取微信小程序(实战篇)
python爬取微信小程序(实战篇) 本文链接:https://blog.csdn.net/HeyShHeyou/article/details/90452656 展开 一.背景介绍 近期有需求需要抓 ...
- Python爬取微信小程序(Charles)
Python爬取微信小程序(Charles) 本文链接:https://blog.csdn.net/HeyShHeyou/article/details/90045204 一.前言 最近需要获取微信小 ...
- scrapy爬取微信小程序社区教程(crawlspider)
爬取的目标网站是: http://www.wxapp-union.com/portal.php?mod=list&catid=2&page=1 目的是爬取每一个教程的标题,作者,时间和 ...
- 使用Python爬取微信公众号文章并保存为PDF文件(解决图片不显示的问题)
前言 第一次写博客,主要内容是爬取微信公众号的文章,将文章以PDF格式保存在本地. 爬取微信公众号文章(使用wechatsogou) 1.安装 pip install wechatsogou --up ...
- 微信小程序的ajax数据请求wx.request
微信小程序的ajax数据请求,很多同学找不到api在哪个位置,这里单独把小程序的ajax请求给列出来,微信小程序的请求就是wx.request这个api,wx.request(一些对象参数),微信小程 ...
- 《吐血整理》高级系列教程-吃透Fiddler抓包教程(34)-Fiddler如何抓取微信小程序的包-上篇
1.简介 有些小伙伴或者是童鞋们说小程序抓不到包,该怎么办了???其实苹果手机如果按照宏哥前边的抓取APP包的设置方式设置好了,应该可以轻松就抓到包了.那么安卓手机小程序就比较困难,不是那么友好了.所 ...
- 如何抓取微信小程序的源码?
一.引言: 在工作中我们会想把别人的代码直接拿过来进行参考,当然这个更多的是前端代码的进行获取. 那么微信小程序的代码怎么样获取呢? 参考 https://blog.csdn.net/qq_4113 ...
- 微信小程序文章收录
基础篇 03-04 微信登入小程序与后端实现 - 小猿取经 - 博客园 我做的小程序 - 小y - 博客园 小程序二维码和小程序带参数二维码生成 - Likwo - 博客园 accesstoken 微 ...
随机推荐
- Ubuntu 上网
1.打开终端 2.sudo gedit /etc/wpa_supplicant/wpa_supplicant.conf(回车之后会弹出一个编辑页面,在里面打入第三步里面的配置文件)3.配置文件如下:c ...
- C++性能榨汁机之伪共享
C++性能榨汁机之伪共享 来源 http://irootlee.com/juicer_false_sharing/ 前言 在多核并发编程中,如果将互斥锁的争用比作“性能杀手”的话,那么伪共享则相当于 ...
- redis 交集、并集、差集
sinter .sunion .sdiff redis 支持 Set集合的数据存储,其中有三个比较特殊的方法: sinter key [key …] 返回一个集合的全部成员,该集合是所有给定集合的交集 ...
- adb实操
一.命令 adb connect IP:5555 adb disconnect IP:5555 adb remount adb install 安装包的绝对路径 二.获取logcat信息 1.制作文件 ...
- 安卓开发之Toolbar返回键
本文前三步演示了为Toolbar添加返回键并实现返回的步骤,第四步给出了设置返回键颜色的方法. 1.在xml布局中引用toolbar: <android.support.design.widge ...
- 如何使用Marketing Cloud的扩展字段作为搜索条件进行搜索
需求:我在Marketing Cloud的contact模型上用custom field这个应用创建了一个Extension field,名称为微信ID. 现在客户的需求是使用这个字段作为过滤条件进行 ...
- shell数组处理
linux shell在编程方面比windows 批处理强大太多,无论是在循环.运算.已经数据类型方面都是不能比较的. 下面是个人在使用时候,对它在数组方面一些操作进行的总结. 1.数组定义 ...
- (备忘)Window7下安装Python2.6及Django1.4
1.Python2.6安装 1.1 Python2.6的下载及安装 Python 版本:2.6 下载地址:http://www.python.org/download/releases/2.6.1/ ...
- Ubuntu18.04安装OpenCV4.1.0
Ubuntu18.04安装OpenCV4.1.0 1.首先要安装依赖 sudo apt-get install build-essential \ cmake git libgtk2.0-dev pk ...
- 《wifi加密破解论文》翻译介绍-wifi不再安全
前言 wifi的加密协议WPA2已经被破解,影响范围包括所有支持wifi的设备,包括Android,Linux,Apple,Windows,OpenBSD,联发科技,Linksys等.其中对Andro ...