21天打造分布式爬虫-Crawl类爬取小程序社区(八)
8.1.Crawl的用法实战
新建项目
scrapy startproject wxapp scrapy genspider -t crawl wxapp_spider "wxapp-union.com"
wxapp_spider.py
# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from wxapp.items import WxappItem class 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=\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)
return item
items.py
# -*- coding: utf-8 -*- import scrapy class WxappItem(scrapy.Item):
title = scrapy.Field()
author = scrapy.Field()
pub_time = scrapy.Field()
content = scrapy.Field()
pipelines.py
# -*- coding: utf-8 -*- from scrapy.exporters import JsonLinesItemExporter class WxappPipeline(object):
def __init__(self):
self.fp = open('wxapp.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 item def close_spider(self, spider):
self.fp.close()
settings.py
ROBOTSTXT_OBEY = False
DOWNLOAD_DELAY = 1
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 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36',
}
ITEM_PIPELINES = {
'wxapp.pipelines.WxappPipeline': 300,
}
start.py
from scrapy import cmdline
cmdline.execute("scrapy crawl wxapp_spider".split())
21天打造分布式爬虫-Crawl类爬取小程序社区(八)的更多相关文章
- 21天打造分布式爬虫-Spider类爬取糗事百科(七)
7.1.糗事百科 安装 pip install pypiwin32 pip install Twisted-18.7.0-cp36-cp36m-win_amd64.whl pip install sc ...
- 21天打造分布式爬虫-Selenium爬取拉钩职位信息(六)
6.1.爬取第一页的职位信息 第一页职位信息 from selenium import webdriver from lxml import etree import re import time c ...
- 21天打造分布式爬虫-urllib库(一)
1.1.urlopen函数的用法 #encoding:utf-8 from urllib import request res = request.urlopen("https://www. ...
- 21天打造分布式爬虫-requests库(二)
2.1.get请求 简单使用 import requests response = requests.get("https://www.baidu.com/") #text返回的是 ...
- 爬虫实战——Scrapy爬取伯乐在线所有文章
Scrapy简单介绍及爬取伯乐在线所有文章 一.简说安装相关环境及依赖包 1.安装Python(2或3都行,我这里用的是3) 2.虚拟环境搭建: 依赖包:virtualenv,virtualenvwr ...
- 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...
- Python爬虫实战之爬取百度贴吧帖子
大家好,上次我们实验了爬取了糗事百科的段子,那么这次我们来尝试一下爬取百度贴吧的帖子.与上一篇不同的是,这次我们需要用到文件的相关操作. 本篇目标 对百度贴吧的任意帖子进行抓取 指定是否只抓取楼主发帖 ...
- 爬虫入门之爬取策略 XPath与bs4实现(五)
爬虫入门之爬取策略 XPath与bs4实现(五) 在爬虫系统中,待抓取URL队列是很重要的一部分.待抓取URL队列中的URL以什么样的顺序排列也是一个很重要的问题,因为这涉及到先抓取那个页面,后抓取哪 ...
- Python 网络爬虫 002 (入门) 爬取一个网站之前,要了解的知识
网站站点的背景调研 1. 检查 robots.txt 网站都会定义robots.txt 文件,这个文件就是给 网络爬虫 来了解爬取该网站时存在哪些限制.当然了,这个限制仅仅只是一个建议,你可以遵守,也 ...
随机推荐
- React createRef:引用
一 代码 import React, { Component } from 'react'; class Box extends Component { render() { return <b ...
- 100-days: nineteen
Title: Figure skating(花样滑冰): Olympic(奥林匹克的) champion Hanyu says '100 percent' on(引出时机) return to ice ...
- 396. Rotate Function 移动加权求和,取最大值
[抄题]: Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by ...
- js的一些总结
函数 函数是一等公民 函数可以有属性,并且能连接到它的构造方法 函数可以像一个变量一样存在内存中 函数可以当做参数传给其他函数 函数可以返回其他函数 加法操作表 Number + Number -&g ...
- http协议和四个层之间的关系
TCP/IP协议的分层:应用层.传输层.网络层.数据链路层. ····应用层···· 决定了向用户提供应用服务时通信的活动.HTTP协议存在于该层.(FTP文件传输协议,DNS域名系统) ....传输 ...
- Android通过手机搭建服务器,WIFI建立热点实现C/S聊天室通信功能
应用效果图: 客户端 ...
- cpp 区块链模拟示例(四) 区块链工作量证明
本文主要在之前的区块链原形上添加了工作量证明,并且为后继的交易功能做好准备. 上一个章节我们已经创建了区块链的基本原形,但是区块的哈希计算和加入太过于简单,如果按照这种速度添加区块那么区块链估计一个小 ...
- Uncommon Words from Two Sentences LT884
We are given two sentences A and B. (A sentence is a string of space separated words. Each word co ...
- 别人的Linux私房菜(10)vim程序编辑器
很多软件的编辑接口会主动调用vi vi分一般命令模式.编辑模式.命令行模式. 使用vi :/bin/vi welcome.txt 下下端显示文本有多少行,多少字符, 一般命令模式: 上下左右移动光标k ...
- string所在头文件
使用string.wstring 头文件:#include <string> 命名空间:std