资讯专栏INFORMATION COLUMN

scrapy学习之路2(图片下载与下载的路径获取)

WelliJhon / 680人阅读

摘要:图片下载和拿到下载后的路径小封面图的爬取,后面通过传到中详情页的爬取详情页的完整地址下一页的爬取与请求不明打开功能注意如要进一步定制功能补充新建

图片下载和拿到下载后的路径 1

items.py

import scrapy

class InfoItem(scrapy.Item):
    url = scrapy.Field()
    url_object_id = scrapy.Field()
    small_image = scrapy.Field()
    small_image_path = scrapy.Field()
    big_image = scrapy.Field()
    big_image_path = scrapy.Field()
    code = scrapy.Field()
    date = scrapy.Field()
    lengths = scrapy.Field()
    author = scrapy.Field()
    cate = scrapy.Field()
    av_artor = scrapy.Field()

spider/jxxx.py

# -*- coding: utf-8 -*-
import scrapy
from urllib import parse
from scrapy.http import Request
from JaSpider.items import InfoItem
from JaSpider.utils.common import get_md5


class JxxxSpider(scrapy.Spider):
    name = "jxxx"
    allowed_domains = ["www.jxxx.com"]
    start_urls = ["http://www.jxxx.com/cn/vl_update.php"]

    def parse(self, response):
        for i in response.css(".video"):
            small_image = i.css("img::attr(src)").extract_first() # 小封面图的爬取,后面通过meta传到parse_info中
            link = i.css("a::attr(href)").extract_first() # 详情页的url爬取
            real_url = parse.urljoin(response.url, link) # 详情页的完整地址
            yield Request(url=real_url, meta={"small_image": small_image}, callback=self.parse_info)
        # 下一页的爬取与请求    
        next_url = response.css(".page_selector .page.next::attr(href)").extract_first()
        perfect_next_url = parse.urljoin(response.url, next_url)
        if next_url:
            yield Request(url=perfect_next_url, callback=self.parse)

    def parse_info(self, response):
        small_image = "http:"+response.meta["small_image"]
        big_image = "http:"+response.xpath("//div[@id="video_jacket"]/img/@src").extract_first()
        code = response.css("#video_id .text::text").extract_first()
        date = response.css("#video_date .text::text").extract_first()
        lengths = response.css("#video_length .text::text").extract_first()
        author = response.css("#video_director .director a::text").extract_first() if response.css("#video_director .director a::text").extract_first() else "不明"
        cate = ",".join([i.css("a::text").extract_first() for i in response.css("#video_genres .genre") if i.css("a::text").extract_first()])
        av_artor = ",".join([i.css("a::text").extract_first() for i in response.css(".star") if i.css("a::text").extract_first()])
        # print("http:"+small_image)
        info_item = InfoItem()
        info_item["url"] = response.url
        info_item["url_object_id"] = get_md5(response.url)
        info_item["small_image"] = small_image
        info_item["big_image"] = [big_image]
        info_item["code"] = code
        info_item["date"] = date
        info_item["lengths"] = lengths
        info_item["author"] = author
        info_item["cate"] = cate
        info_item["av_artor"] = av_artor
        yield info_item
2

打开pipeline功能 settings.py


注意!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!:
spider/jxxx.py

3

如要进一步定制功能
settings.py

pipeline.py

4

补充
新建utils/common.py

import hashlib


def get_md5(url):
    if isinstance(url, str):
        url = url.encode("utf-8")
    m = hashlib.md5()
    m.update(url)
    return m.hexdigest()


if __name__ == "__main__":
    a = get_md5("http://www.haddu.com")
    print(a)

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/41201.html

相关文章

  • scrapy 学习之路那些坑

    摘要:前言本文记录自己在学习当中遇到的各种大小问题,持续更新。错误分析本身是一个网络引擎框架,的运行依赖于。在打开新建的项目后,报错显示。错误分析的默认依赖项当中没有,或者说默认查找的路径中找不到。 前言 本文记录自己在学习scrapy当中遇到的各种大小问题,持续更新。 环境简介: 语言版本 爬虫框架 IDE 系统 python3.5 scrapy1.4.0 pycharm win1...

    xiaodao 评论0 收藏0
  • scrapy学习之路1(简单例子)

    摘要:的安装环境是后面创建用来运行的名网站域名在创建可以通过此文件运行本文件名父文件名路径和父文件名设置环境,必须以上运行可能在下会报错准备工作完在下获取列表页每一个的把获取到的交给 scrapy的安装 环境:python3.6 1 pip install -i https://pypi.douban.com/simple/ scrapy 2 scrapy startpr...

    guqiu 评论0 收藏0
  • 20、 Python快速开发分布式搜索引擎Scrapy精讲—编写spiders爬虫文件循环抓取内容

    摘要:百度云搜索,搜各种资料搜网盘,搜各种资料编写爬虫文件循环抓取内容方法,将指定的地址添加到下载器下载页面,两个必须参数,参数页面处理函数使用时需要方法,是库下的方法,是自动拼接,如果第二个参数的地址是相对路径会自动与第一个参数拼接导 【百度云搜索,搜各种资料:http://bdy.lqkweb.com】 【搜网盘,搜各种资料:http://www.swpan.cn】 编写spiders爬...

    CntChen 评论0 收藏0
  • windows下安装python+scrapy

    摘要:好啦一切准备工作就绪,现在开始安装库安装成功后,安装就简单了,在命令提示符窗口直接输入命令回车现在一切都搞定了,可以新建一个测试,敲一个基于框架的爬虫程序咯。 最近忽然有了想要学习python爬虫的想法,但是首先需要安装工具。python安装倒是很轻松,只要傻瓜式一键安装即可,但是在Windows下安装scrapy倒不是件容易的事情。言归正传,说下我从昨天下午到今天上午安装的步骤: 1...

    sevi_stuo 评论0 收藏0
  • windows下安装python+scrapy

    摘要:好啦一切准备工作就绪,现在开始安装库安装成功后,安装就简单了,在命令提示符窗口直接输入命令回车现在一切都搞定了,可以新建一个测试,敲一个基于框架的爬虫程序咯。 最近忽然有了想要学习python爬虫的想法,但是首先需要安装工具。python安装倒是很轻松,只要傻瓜式一键安装即可,但是在Windows下安装scrapy倒不是件容易的事情。言归正传,说下我从昨天下午到今天上午安装的步骤: 1...

    dantezhao 评论0 收藏0

发表评论

0条评论

WelliJhon

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<