8.1.Crawl的用法实战

新建项目

  1. scrapy startproject wxapp
  2.  
  3. scrapy genspider -t crawl wxapp_spider "wxapp-union.com"

wxapp_spider.py

  1. # -*- coding: utf-8 -*-
  2. import scrapy
  3. from scrapy.linkextractors import LinkExtractor
  4. from scrapy.spiders import CrawlSpider, Rule
  5. from wxapp.items import WxappItem
  6.  
  7. class WxappSpiderSpider(CrawlSpider):
  8. name = 'wxapp_spider'
  9. allowed_domains = ['wxapp-union.com']
  10. start_urls = ['http://www.wxapp-union.com/portal.php?mod=list&catid=2&page=1']
  11.  
  12. rules = (
  13. Rule(LinkExtractor(allow=r'.+mod=list&catid=\d'), follow=True),
  14. Rule(LinkExtractor(allow=r'.+article-.+\.html'), callback="parse_detail",follow=False),
  15. )
  16.  
  17. def parse_detail(self, response):
  18. title = response.xpath("//h1[@class='ph']/text()").get()
  19. author_p = response.xpath("//p[@class='authors']")
  20. author = author_p.xpath(".//a/text()").get()
  21. pub_time = author_p.xpath(".//span/text()").get()
  22. article_content = response.xpath("//td[@id='article_content']//text()").getall()
  23. content = "".join(article_content).strip()
  24. item = WxappItem(title=title,author=author,pub_time=pub_time,content=content)
  25. return item

items.py

  1. # -*- coding: utf-8 -*-
  2.  
  3. import scrapy
  4.  
  5. class WxappItem(scrapy.Item):
  6. title = scrapy.Field()
  7. author = scrapy.Field()
  8. pub_time = scrapy.Field()
  9. content = scrapy.Field()

pipelines.py

  1. # -*- coding: utf-8 -*-
  2.  
  3. from scrapy.exporters import JsonLinesItemExporter
  4.  
  5. class WxappPipeline(object):
  6. def __init__(self):
  7. self.fp = open('wxapp.json','wb')
  8. self.exporter = JsonLinesItemExporter(self.fp, ensure_ascii=False, encoding='utf-8')
  9.  
  10. def process_item(self, item, spider):
  11. self.exporter.export_item(item)
  12. return item
  13.  
  14. def close_spider(self, spider):
  15. self.fp.close()

settings.py

  1. ROBOTSTXT_OBEY = False
  2.  
  3. DOWNLOAD_DELAY = 1
  4.  
  5. DEFAULT_REQUEST_HEADERS = {
  6. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  7. 'Accept-Language': 'en',
  8. 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36',
  9. }
  10.  
  11. ITEM_PIPELINES = {
  12. 'wxapp.pipelines.WxappPipeline': 300,
  13. }

start.py

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

21天打造分布式爬虫-Crawl类爬取小程序社区(八)的更多相关文章

  1. 21天打造分布式爬虫-Spider类爬取糗事百科(七)

    7.1.糗事百科 安装 pip install pypiwin32 pip install Twisted-18.7.0-cp36-cp36m-win_amd64.whl pip install sc ...

  2. 21天打造分布式爬虫-Selenium爬取拉钩职位信息(六)

    6.1.爬取第一页的职位信息 第一页职位信息 from selenium import webdriver from lxml import etree import re import time c ...

  3. 21天打造分布式爬虫-urllib库(一)

    1.1.urlopen函数的用法 #encoding:utf-8 from urllib import request res = request.urlopen("https://www. ...

  4. 21天打造分布式爬虫-requests库(二)

    2.1.get请求 简单使用 import requests response = requests.get("https://www.baidu.com/") #text返回的是 ...

  5. 爬虫实战——Scrapy爬取伯乐在线所有文章

    Scrapy简单介绍及爬取伯乐在线所有文章 一.简说安装相关环境及依赖包 1.安装Python(2或3都行,我这里用的是3) 2.虚拟环境搭建: 依赖包:virtualenv,virtualenvwr ...

  6. 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神

    原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...

  7. Python爬虫实战之爬取百度贴吧帖子

    大家好,上次我们实验了爬取了糗事百科的段子,那么这次我们来尝试一下爬取百度贴吧的帖子.与上一篇不同的是,这次我们需要用到文件的相关操作. 本篇目标 对百度贴吧的任意帖子进行抓取 指定是否只抓取楼主发帖 ...

  8. 爬虫入门之爬取策略 XPath与bs4实现(五)

    爬虫入门之爬取策略 XPath与bs4实现(五) 在爬虫系统中,待抓取URL队列是很重要的一部分.待抓取URL队列中的URL以什么样的顺序排列也是一个很重要的问题,因为这涉及到先抓取那个页面,后抓取哪 ...

  9. Python 网络爬虫 002 (入门) 爬取一个网站之前,要了解的知识

    网站站点的背景调研 1. 检查 robots.txt 网站都会定义robots.txt 文件,这个文件就是给 网络爬虫 来了解爬取该网站时存在哪些限制.当然了,这个限制仅仅只是一个建议,你可以遵守,也 ...

随机推荐

  1. POJ-2253.Frogger.(求每条路径中最大值的最小值,最短路变形)

    做到了这个题,感觉网上的博客是真的水,只有kuangbin大神一句话就点醒了我,所以我写这篇博客是为了让最短路的入门者尽快脱坑...... 本题思路:本题是最短路的变形,要求出最短路中的最大跳跃距离, ...

  2. H3C网络设备配置SSH登录

    使用SSH+密码认证(基本SSH配置方法)注:在用户使用SSH登录交换机时,交换机对所要登录的用户使用密码对其进行身份验证生成RSA和DSA密钥对[H3C]public-key local creat ...

  3. AltiumDesigner 常用快捷键小结

    Ctrl + o  |  打开文件夹/文档 Ctrl + p  |  打印设置 Esc   |  从当前步骤退出 Shift +鼠标滚轮  |  向左/向右移动 Ctrl + C (或 Ctrl + ...

  4. 508. Most Frequent Subtree Sum 最频繁的子树和

    [抄题]: Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum ...

  5. nodejs+https 使用openssl (window)

    HTML的getUsermedia必是要安全的连接 比如 localhost.127.0.0.1 .https chrome才让调用摄像头 1.申请域名.备案.域名解析 2.openssl生成 打开g ...

  6. Solidity的三种合约间的调用方式 call、delegatecall 和 callcode

    0x00 前言 Solidity(http://solidity.readthedocs.io/en/v0.4.24/) 是一种用与编写以太坊智能合约的高级语言,语法类似于 JavaScript. S ...

  7. 二、putty的下载安装和基本使用方法教程

    转载自:https://baijiahao.baidu.com/s?id=1597811787635071952&wfr=spider&for=pc PuTTY是一款开源(Open S ...

  8. Linux系统中的tar命令

    时间一长什么东西都容易忘记,尤其是一些不常用的东西忘记的更快,所以避免忘记,就记录下来,可以方面使用的时候查询.Tar命令在linux系统中算是一个比较重要的命令,今天就针对该命令进行总结一下. 1. ...

  9. Python开发——【条件】语句

    单分支 # if 条件: # 满足条件后要执行的代码 if 2>1: print("2>1") 双分支 # if 条件: # 满足条件后要执行的代码 # else: # ...

  10. Android通过手机搭建服务器,WIFI建立热点实现C/S聊天室通信功能

    应用效果图:                                                客户端                                            ...