用scrapy框架爬取映客直播用户头像

1. 创建项目 scrapy startproject yingke cd yingke

2. 创建爬虫  scrapy genspider live

3. 分析http://www.inke.cn/hotlive_list.html网页的response,找到响应里面数据的规律,并找到的位置,通过response.xpath()获取到

4. 通过在pipline里面进行数据的清洗,过滤,保存

5. 实现翻页,进行下一页的请求处理

6. 运行爬虫 scrapy crawl live

说明:这个程序直接在parse方法里面进行图片保存,保存在本地,正常使用yield关键字进行在pipline中保存。

# -*- coding: utf-8 -*-
import scrapy
import re


class LiveSpider(scrapy.Spider):
    name = 'live'
    allowed_domains = ['inke.cn']
    start_urls = ['http://www.inke.cn/hotlive_list.html?page=1']

    def parse(self, response):
        div_list = response.xpath("//div[@class='list_box']")

        for div in div_list:
            item = {}
            img_src = div.xpath("./div[@class='list_pic']/a/img/@src").extract_first()
            item["user_name"] = div.xpath(
                "./div[@class='list_user_info']/span[@class='list_user_name']/text()").extract_first()
            print(item["user_name"])
            yield scrapy.Request(  # 发送详情页的请求
                img_src,
                callback=self.parse_img,
                meta={"item": item}
            )
        # 下一页
        now_page = re.findall("page=(.*)", response.request.url)[0]
        now_page= int(now_page)

        next_url = "http://www.inke.cn/hotlive_list.html?page={}".format(str(now_page+ 1))
        yield scrapy.Request(
            next_url,
            callback=self.parse
        )

    def parse_img(self, response):
        user_name = response.meta["item"]["user_name"]

        with open("images/{}.png".format(user_name), "wb") as f:

            f.write(response.body)

运行效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值