###CrawSpider:

创建CrawlSpider爬虫:

之前创建爬虫的方式是通过scrapy genspider [爬虫名字] [域名]的方式创建的。如果想要创建CrawlSpider爬虫,那么应该通过以下命令创建:

scrapy genspider -t crawl [爬虫名字] [域名]

LinkExtractors链接提取器:

使用LinkExtractors可以不用程序员自己提取想要的url,然后发送请求。这些工作都可以交给LinkExtractors,他会在所有爬的页面中找到满足规则的url,实现自动的爬取。以下对LinkExtractors类做一个简单的介绍:

class scrapy.linkextractors.LinkExtractor(allow = (),deny = (),allow_domains = (),deny_domains = (),deny_extensions = None,restrict_xpaths = (),tags = ('a','area'),attrs = ('href'),canonicalize = True,unique = True,process_value = None
)

主要参数讲解:

  • allow:允许的url。所有满足这个正则表达式的url都会被提取。
  • deny:禁止的url。所有满足这个正则表达式的url都不会被提取。
  • allow_domains:允许的域名。只有在这个里面指定的域名的url才会被提取。
  • deny_domains:禁止的域名。所有在这个里面指定的域名的url都不会被提取。
  • restrict_xpaths:严格的xpath。和allow共同过滤链接。

Rule规则类:

定义爬虫的规则类。以下对这个类做一个简单的介绍:

class scrapy.spiders.Rule(link_extractor, callback = None, cb_kwargs = None, follow = None, process_links = None, process_request = None
)

主要参数讲解:

  • link_extractor:一个LinkExtractor对象,用于定义爬取规则。
  • callback:满足这个规则的url,应该要执行哪个回调函数。因为CrawlSpider使用了parse作为回调函数,因此不要覆盖parse作为回调函数自己的回调函数。
  • follow:指定根据该规则从response中提取的链接是否需要跟进。
  • process_links:从link_extractor中获取到链接后会传递给这个函数,用来过滤不需要爬取的链接。

1 allow设置规则的方法:

要能够限制在想要的url上面,不要跟别的url 产生相同 的正则。
2.什么情况下使用follow:
爬取页面时,需要将当前条件的Url进行推进,则为true,否则是Fasle
3.什么情况下用callback:
需要爬取该页面的详细数据时,用true,否则不用指定。

下面看代码:
wxapp.spider.py

# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from wxapp.items import WxappItemclass WxappSpiderSpider(CrawlSpider):name = 'wxapp_spider'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):title=response.xpath("//h1[@class='ph']/text()").get()author_p=response.xpath("//p[@class='authors']")author=author_p.xpath(".//a/text()").get()pub_time=author_p.xpath(".//span/text()").get()article_content=response.xpath("//td[@id='article_content']//text()").getall()content="".join(article_content).strip()item=WxappItem(title=title,author=author,pub_time=pub_time,content=content)yield item

items.py

# -*- coding: utf-8 -*-# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.htmlimport scrapyclass WxappItem(scrapy.Item):title=scrapy.Field()author=scrapy.Field()pub_time=scrapy.Field()content=scrapy.Field()pass

piplines.py

# -*- coding: utf-8 -*-# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy.exporters import JsonLinesItemExporterclass WxappPipeline(object):def __init__(self):self.fp=open("wxjc.json","wb")self.exporter=JsonLinesItemExporter(self.fp,ensure_ascii=False,encoding='utf-8')def process_item(self, item, spider):self.exporter.export_item(item)return itemdef close_spider(self,spider):self.fp.close()

settings.py

# -*- coding: utf-8 -*-# Scrapy settings for wxapp project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://docs.scrapy.org/en/latest/topics/settings.html
#     https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://docs.scrapy.org/en/latest/topics/spider-middleware.htmlBOT_NAME = 'wxapp'SPIDER_MODULES = ['wxapp.spiders']
NEWSPIDER_MODULE = 'wxapp.spiders'# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'wxapp (+http://www.yourdomain.com)'# Obey robots.txt rules
ROBOTSTXT_OBEY = False# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32# Configure a delay for requests for the same website (default: 0)
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
DOWNLOAD_DELAY = 1
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16# Disable cookies (enabled by default)
#COOKIES_ENABLED = False# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False# Override the default request headers:
DEFAULT_REQUEST_HEADERS = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language': 'en',
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36"
}# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {#    'wxapp.middlewares.WxappSpiderMiddleware': 543,
#}# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {#    'wxapp.middlewares.WxappDownloaderMiddleware': 543,
#}# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {#    'scrapy.extensions.telnet.TelnetConsole': None,
#}# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {'wxapp.pipelines.WxappPipeline': 300,
}# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

start.py

from scrapy import cmdline
cmdline.execute("scrapy crawl wxapp_spider".split())

如下有保存的数据截图:

不得不说微信小程序看起来还是挺友好的呢。
下次想冲一下微信小程序。

[python爬虫之路day20]:CrawSpider爬取微信小程序社区技术帖相关推荐

  1. python爬取微信小程序(实战篇)_爬虫爬取微信小程序

    之前打算做个微信小程序的社区,所以写了爬虫去爬取微信小程序,后面发现做微信小程序没有前途,就把原来的项目废弃了做了现在的网站观点,不过代码放着也是放着,还不如公开让大家用,所以我把代码贴出来,有需要的 ...

  2. python爬虫爬取微信_Python爬虫爬取微信小程序

    之前打算做个微信小程序的社区,所以写了爬虫去爬取微信小程序,后面发现做微信小程序没有前途,就把原来的项目废弃了做了现在的网站观点,不过代码放着也是放着,还不如公开让大家用,所以我把代码贴出来,有需要的 ...

  3. python爬取微信小程序源代码_爬虫爬取微信小程序

    之前打算做个微信小程序的社区,所以写了爬虫去爬取微信小程序,后面发现做微信小程序没有前途,就把原来的项目废弃了做了现在的网站观点,不过代码放着也是放着,还不如公开让大家用,所以我把代码贴出来,有需要的 ...

  4. 微信小程序爬虫python_爬虫爬取微信小程序

    之前打算做个微信小程序的社区,所以写了爬虫去爬取微信小程序,后面发现做微信小程序没有前途,就把原来的项目废弃了做了现在的网站观点,不过代码放着也是放着,还不如公开让大家用,所以我把代码贴出来,有需要的 ...

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

    一.背景介绍 近期有需求需要抓取微信小程序中的数据分析,与一般的网页爬虫类似,主要目标是获取主要的URL地址进行数据爬取,而问题的关键在于如何获取移动端request请求后https加密的参数.本文从 ...

  6. 利用fiddler抓包爬取微信小程序数据

    利用fiddler抓包爬取微信小程序数据 1.背景原理 有些微信小程序无法在PC端进行访问 原因 判断非微信'内嵌浏览器',则禁止访问 解决方法 模拟微信'内嵌浏览器'进行访问,需要获取的数据有:Us ...

  7. Python爬虫开源项目代码(爬取微信、淘宝、豆瓣、知乎、新浪微博、QQ、去哪网 等等)...

    文章目录 1.简介 2.开源项目Github 2.1.WechatSogou [1]– 微信公众号爬虫 2.2.DouBanSpider [2]– 豆瓣读书爬虫 2.3.zhihu_spider [3 ...

  8. Python爬虫开源项目代码(爬取微信、淘宝、豆瓣、知乎、新浪微博、QQ、去哪网 等等)

    文章目录 1.简介 2.开源项目Github 2.1.WechatSogou [1]– 微信公众号爬虫 2.2.DouBanSpider [2]– 豆瓣读书爬虫 2.3.zhihu_spider [3 ...

  9. 23个Python爬虫开源项目代码:爬取微信、淘宝、豆瓣、知乎、微博

    今天为大家整理了32个Python爬虫项目.整理的原因是,爬虫入门简单快速,也非常适合新入门的小伙伴培养信心,所有链接指向GitHub. 1.WechatSogou – 微信公众号爬虫 基于搜狗微信搜 ...

  10. 推荐23个Python爬虫开源项目代码:爬取微信、淘宝、豆瓣、知乎、微博等

    点击上方 Python知识圈,选择"设为星标" 回复"1024"获取编程资料 阅读文本大概需要 5 分钟. 今天为大家整理了23个Python爬虫项目.整理的原 ...

最新文章

  1. 猖狂!微软、思科源码惨遭黑客 100 万美元打包出售
  2. C#序列化反序列化对象为base64字符串
  3. JMeter 测试计划
  4. spirngmvc如何实现直接输入网页重定向到登录_Python 模拟新浪微博登录
  5. spring3依赖包下载
  6. 关于js对象引用的小例子
  7. numpy 矩阵与向量相乘_有人把NumPy画成了花,生动又形象
  8. 华为2017.7.26机试
  9. Mybatis bug修正
  10. Node.js CVE-2017-14849复现(详细步骤)
  11. Selenium-WebDriver驱动对照表
  12. [项目实战篇] Emos在线办公小程序--搭建项目
  13. IE下载vsix插件踩坑
  14. 无线认证 服务器是怎么回事,无线wifi认证服务器参数设置方法是什么
  15. java wms erp自动化立体仓库管理系统 进出库 源码 源代码 程序
  16. 深究:app如何实现即时通讯
  17. java8,java9和java11的特性和区别!
  18. 对AVL树和红黑树的个人理解
  19. Quillbot:英语到英语的屠龙剑
  20. 微信网页版传输助手上线

热门文章

  1. 美国弗吉尼亚大学计算机科学,弗吉尼亚大学UVa计算机科学Computer Science专业排名第201-250位(2021年THE世界大学商科排名)...
  2. linux权限英文,Linux常见英文报错中文翻译(菜鸟必知)
  3. 计算机专业纸质笔记本,无可替代?信息时代你还用纸质笔记本吗
  4. 中国超级计算机之最,中国超级计算机神威太湖之光世界最快,且总量排名榜单第一...
  5. 查询某个网址的服务器IP
  6. android webview 手机兼容问题
  7. 幼儿园计算机基础知识,幼儿园大班计算机教学计划
  8. 云计算的1024种玩法——如何快速搭建个人博客?
  9. 深圳一普通中学老师工资单曝光,秒杀程序员
  10. (摘之博客园狂奔di蜗牛)ASP.NET页面刷新方法总结