CrawlSpider爬取微信文章

需要使用LinkExtractor和Rule,这两个东西决定爬虫的具体走向

1.allow设置规则的方法,要能够限制在我们想要的url上面,不要和其他的url产生相同的正则表达式即可

2.啥情况下使用follow, 如果在爬取页面的时候需要将当前的url在进行更近,那么就设置为True,否者设置为False

3.啥情况下该指定callback,如果这个url对应的页面为了获取更多的url,并不需要获取里面的数据,那么可以不指定callback,如果想要获取url对应页面中的数据,那么就需要指定一个callback

创建crawlspider爬虫文件

scrapy genspider crawl -t 爬虫名称 限定域

items.py文件

import scrapy

class WechatItem(scrapy.Item):
    title = scrapy.Field()
    auth = scrapy.Field()
    time = scrapy.Field()
    content = scrapy.Field()

piplines.py文件

from scrapy.exporters import JsonLinesItemExporter


class WechatPipeline(object):

    def __init__(self):
        self.fp = open('wechat.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 open_spider(self, spider):
        print("爬虫开始了")

    def close_spider(self, spider):
        print("爬虫结束了")
        self.fp.close()

wechatspider.py

# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

from wechat.items import WechatItem

class WechatSpiderSpider(CrawlSpider):
  name = 'wechat_spider'
  allowed_domains = ['wxapp-union.com']
  start_urls = ['http://www.wxapp-union.com/'
                'portal.php?mod=list&catid=2&page=1']
  # rules规则如何编写
  # allow
  # 允许爬取的链接规则,可以使用正则
  # callback
  # 不需要解析的话不用添加回调函数
  # follow
  # 解析过后不用再跟踪页面中相应的链接,就选择False
  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()
      auth_time = response.xpath("//p[@class='authors']")
      auth = auth_time.xpath(".//a/text()").get()
      time = auth_time.xpath(".//span/text()").get()
      article = response.xpath('//td[@id="article_content"]//text()').getall()
      content = "".join(article).strip()
      # print("********************")
      # print(title, auth, time, article)
      # print("********************")

      item = WechatItem(title=title, auth=auth, time=time, content=content)
      yield item

若有建议评论区留言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值