zoukankan      html  css  js  c++  java
  • Python抓取妹子图,内含福利

    目标抓取全站妹子封面图片全部爬下来以图片标题命名
    1. 分析网页数据结构


       
      妹子图首页
    2. 接下来找张图片右击点击检查


       
      想要数据
    3. 拿到图片链接直接用浏览器可以访问,但是程序下载有反爬虫,图片直接下载不了需要加请求头部信息
       
       

      先上手代码试试!
    import requests
    from lxml import etree
    
    
    # 设计模式 --》面向对象编程
    class Spider(object):
        def __init__(self):
            # 反反爬虫措施,加请求头部信息
            self.headers = {
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36",
                "Referer": "https://www.mzitu.com/xinggan/"
            }
    
        def start_request(self):
            # 1. 获取整体网页的数据 requests
            for i in range(1, 204):
                print("==========正在抓取%s页==========" % i)
                response = requests.get("https://www.mzitu.com/page/"+ str(i) + "/", headers=self.headers)
                html = etree.HTML(response.content.decode())
                self.xpath_data(html)
    
        def xpath_data(self, html):
            # 2. 抽取想要的数据 标题 图片 xpath
            src_list = html.xpath('//ul[@id="pins"]/li/a/img/@data-original')
            alt_list = html.xpath('//ul[@id="pins"]/li/a/img/@alt')
            for src, alt in zip(src_list, alt_list):
                file_name = alt + ".jpg"
                response = requests.get(src, headers=self.headers)
                print("正在抓取图片:" + file_name)
                # 3. 存储数据 jpg with open
                try:
                    with open(file_name, "wb") as f:
                        f.write(response.content)
                except:
                    print("==========文件名有误!==========")
    
    
    spider = Spider()
    spider.start_request()
    

      

    哎!好像没问题!

     
    运行中
     
    运行结果
    同学们,都把裤子给我穿上!好好学习!
     
     
    教程已出,但是可能有很多网友不会使用
    大家有任何问题可以扫描二维码关注公众号,添加我的微信
    我会第一时间为大家解答

     搜索公众号“一条正弦”或扫码关注公众号,第一时间获取更多优质资源

  • 相关阅读:
    为https请求配置ssl(不用keystore,直接用证书,rsa私钥,java代码)
    http请求对于List类型参数的处理
    java中string转ByteBuffer
    lua for循环如何从第0位开始
    lua中的cjson简单使用
    mongodb返回方便读的数据
    markdown简单插入图片
    #问题#java报Annotation processing is not supported for module cycles
    #问题#java报can't create non-static inner class instance
    git commit+push的完整步骤
  • 原文地址:https://www.cnblogs.com/girliswater/p/11152942.html
Copyright © 2011-2022 走看看