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())

原文地址:https://www.cnblogs.com/derek1184405959/p/9425234.html

时间: 2024-08-25 08:12:01

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

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

6.1.爬取第一页的职位信息 第一页职位信息 from selenium import webdriver from lxml import etree import re import time class LagouSpider(object): def __init__(self): self.driver = webdriver.Chrome() #python职位 self.url = 'https://www.lagou.com/jobs/list_python?labelWords

21天打造分布式爬虫-房天下全国658城市房源(十一)

项目:爬取房天下网站全国所有城市的新房和二手房信息 网站url分析 1.获取所有城市url http://www.fang.com/SoufunFamily.htm 例如:http://cq.fang.com/ 2.新房url http://newhouse.sh.fang.com/house/s/ 3.二手房url http://esf.sh.fang.com/ 4.北京新房和二手房url规则不同 http://newhouse.fang.com/house/s/ http://esf.fan

21天打造分布式爬虫-数据解析实战(三)

3.1.豆瓣电影 使用lxml import requests from lxml import etree headers = { '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', 'Referer':'https://movie.douban.com/' } url = 'https

21天打造分布式爬虫-中国天气网实战(四)

4.1.中国天气网 网址:http://www.weather.com.cn/textFC/hb.shtml 解析:BeautifulSoup4 爬取所有城市的最低天气 import requests from bs4 import BeautifulSoup import html5lib def parse_page(url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/53

21天打造分布式爬虫(一)

1.1.urlopen函数的用法 #encoding:utf-8 from urllib import request res = request.urlopen("https://www.cnblogs.com/") print(res.readlines()) #urlopen的参数 #def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, # *, cafile=None, capath=None,

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

1.1.urlopen函数的用法 #encoding:utf-8 from urllib import request res = request.urlopen("https://www.cnblogs.com/") print(res.readlines()) #urlopen的参数 #def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, # *, cafile=None, capath=None,

【Python3爬虫】爬取美女图新姿势--Redis分布式爬虫初体验

一.写在前面 之前写的爬虫都是单机爬虫,还没有尝试过分布式爬虫,这次就是一个分布式爬虫的初体验.所谓分布式爬虫,就是要用多台电脑同时爬取数据,相比于单机爬虫,分布式爬虫的爬取速度更快,也能更好地应对IP的检测.本文介绍的是利用Redis数据库实现的分布式爬虫,Redis是一种常用的菲关系型数据库,常用数据类型包括String.Hash.Set.List和Sorted Set,重要的是Redis支持主从复制,主机能将数据同步到从机,也就能够实现读写分离.因此我们可以利用Redis的特性,借助req

[Python爬虫] Selenium爬取新浪微博移动端热点话题及评论 (下)

这篇文章主要讲述了使用python+selenium爬取新浪微博的热点话题和评论信息.其中使用该爬虫的缺点是效率极低,傻瓜式的爬虫,不能并行执行等,但是它的优点是采用分析DOM树结构分析网页源码并进行信息爬取,同时它可以通过浏览器进行爬取中间过程的演示及验证码的输入.这篇文章对爬虫的详细过程就不再论述了,主要是提供可运行的代码和运行截图即可.希望文章对你有所帮助吧~ 参考文章 [python爬虫] Selenium爬取新浪微博内容及用户信息 [Python爬虫] Selenium爬取新浪微博客户

[Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)

一. 文章介绍 前一篇文章"[python爬虫] Selenium爬取新浪微博内容及用户信息"简单讲述了如何爬取新浪微博手机端用户信息和微博信息. 用户信息:包括用户ID.用户名.微博数.粉丝数.关注数等. 微博信息:包括转发或原创.点赞数.转发数.评论数.发布时间.微博内容等. 它主要通过从文本txt中读取用户id,通过"URL+用户ID" 访问个人网站,如柳岩: http://weibo.cn/guangxianliuya 因为手机端数据相对精简简单,所以采用输