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 微 ...
随机推荐
- springboot中的参数传递
1.前端传递到后端 1-1.js function add(){ var obj = {}; obj.parame_empname = $("#EMPNAME").val(); i ...
- sql server 查看表中某一字段的排序规则
SELECT o.name,o.object_id,c.name,c.column_id,c.collation_name FROM sys.columns c JOIN sys.obj ...
- java Spring boot项目简单说明
前言 一直从事.NET开发,但一直有种想去探索Java世界的冲动,今天终于有时间来尝试一下,以下是自己探索过程的简要记录. 一.开发工具 开发工具选用 IntelliJ IDEA社区版(免费),安装教 ...
- 主流RPC框架详解,以及与SOA、REST的区别
什么是RPC RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议. 简言之,RPC使 ...
- Marketing Cloud demo环境和API使用方法说明
version 1.0 作者:Wang Jerry 更多问题请联系我 demo 系统url:https:/jerry.hybris.com/sap/bc/ui5_ui5/ui2/ushell/shel ...
- django 文件上传样例以及遇到的一些问题
使用django上传文件 主流有两种方法 from表单以及ajax,为了自由度高一点,选择了ajax来实现文件的上传 前端部分代码如下: 主要关注 一 有一个文件上传(type='file')的按钮, ...
- uni-app中nvue (weex) 注意事项
前言 uni-app 是 DCloud 出品的新一代跨端框架,可以说是目前跨端数最多的框架之一了,目前支持发布到:App(Android/iOS).H5.小程序(微信小程序/支付宝小程序/百度小程序/ ...
- IO 理论 SOCK理论
IO密集型程序 在程序执行过程中存在大量IO操作,而CPU操作较少,消耗CPU较少,运行效率较低CPU(计算)密集型程序 在程序执行中,CPU运算较多,IO操作相对较少(消耗CPU大,运行速度快)IO ...
- Javascript节点选择
jQuery.parent(expr) 找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$("span").parent(&q ...
- Hadoop_30_MapReduce_多job串联
一个稍复杂点的处理逻辑往往需要多个mapreduce程序串联处理,多job的串联可以借助mapreduce框架的JobControl实现 示例代码: 每个job装配完成才可以进行下面代码: Cont ...