前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >scrapy爬取1024种子

scrapy爬取1024种子

作者头像
LiosWong
发布2019-03-14 15:50:27
2.7K0
发布2019-03-14 15:50:27
举报
文章被收录于专栏:后端沉思录后端沉思录

1024不必多说,老司机都懂,本文介绍scrapy爬取1024种子,代码不到50行!Scrapy,Python开发的一个快速、高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据。Scrapy用途广泛,可以用于数据挖掘、监测和自动化测试。 关于scrapy用下图来说明即可(图片来自https://cuiqingcai.com/3472.html )

scrapy最好的方式通过官方文档,以及社区贡献的中文文档去学习,使用起来也非常简单,当然功能非常强大! 首先创建scrapy项目、CaoliuSpider,下面是创建的爬虫代码:

代码语言:javascript
复制
# -*- coding: utf-8 -*-
import scrapy
import json
from LiosScrapy.items import CaoLiuItem
from scrapy.http.request import Request
import sys

class CaoliuSpider(scrapy.Spider):
    # 爬虫名称
    name = 'caoliu'
    # 涉及到敏感网站地址
    allowed_domains = ['网站地址']
    start_urls = ['请求地址']
    # 列表解析
    def parse(self, response):
        # 通过xpath提取
        node_list = response.xpath("//td[@class='tal']")
        next_page = response.xpath("//a[text()='下一頁']/@href").extract()[0]
        # 遍历列表获取种子名称、详情页URL
        for node in node_list:
            if not len(node.xpath('./h3/a/text()').extract()) or not len(node.xpath('./h3/a/@href').extract()):
                continue
            fileName = node.xpath('./h3/a/text()').extract()[0]
            listUrl = node.xpath('./h3/a/@href').extract()[0]
            # 通过Request meta传递参数
            yield scrapy.Request(self.allowed_domains[0] + "/" + listUrl, callback=self.parseDetail, meta={'fileName': fileName}, dont_filter=True)
        if next_page not in self.allowed_domains[0]:
            yield scrapy.Request(self.allowed_domains[0] + "/" + next_page,callback=self.parse,dont_filter=True)

    # 解析详情页
    def parseDetail(self, response):
        fileName = response.meta['fileName']
        node_list = response.xpath(
            '//a[@style="cursor:pointer;color:#008000;"]')
        for node in node_list:
            yield scrapy.Request(node.xpath('./@href').extract()[0], callback=self.parseDownLoanUrl, meta={'fileName': fileName}, dont_filter=True)
            # 默认获取第一条种子下载地址
            break

    # 拼接种子、下载
    def parseDownLoanUrl(self, response):
        fileName = response.meta['fileName']
        refNode = response.xpath("//input[@type='hidden' and @name='ref']/@value")[0].extract()
        reffNode = response.xpath("//input[@type='hidden' and @name='reff']/@value")[0].extract()
        torrentUrl = "http://www.rmdown.com/download.php?" + "ref="+str(refNode)+ "&reff="+str(reffNode)
        item = CaoLiuItem()
        item['file_urls'] = torrentUrl
        item['file_name'] = fileName+".torrent"
        yield item

Item文件中的代码:

代码语言:javascript
复制
class CaoLiuItem(scrapy.Item):
    # 文件名称
    file_name = scrapy.Field()
    # 指定文件下载的连接
    file_urls = scrapy.Field()
    #文件下载完成后会往里面写相关的信息
    files = scrapy.Field()

管道文件中的代码:

代码语言:javascript
复制
# 继承FilesPipeline,用于下载文件
class CaoLiuPipeline(FilesPipeline):
    def get_media_requests(self, item, info):
        file_url = item['file_urls']
        meta = {'filename': item['file_name']}
        # 交给调度器发送请求
        yield Request(url=file_url, meta=meta)
    # 自定义下载文件的名称    
    def file_path(self, request, response=None, info=None):
        return request.meta.get('filename','')

记得再settings文件中添加管道、以及设置文件存储路径:

代码语言:javascript
复制
ITEM_PIPELINES = {
   # 'LiosScrapy.pipelines.LiosscrapyPipeline': 300,
   'LiosScrapy.pipelines.CaoLiuPipeline': 2
}
FILES_STORE = '/Users/wenchao.wang/LiosWang/sublimetext/LiosScrapy/data/caoliu'

然后执行命令:

代码语言:javascript
复制
scrapy crawl caoliu

终端输出:

打开存储文件夹,发现种子源源不断下载:

scrapy的功能非常强大,以上运用其简单爬取网页信息,作者只用于学习.最后欢迎感兴趣的朋友欢迎一起讨论学习scrapy.

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-02-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 后端沉思录 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档