zoukankan      html  css  js  c++  java
  • 初识Python爬妹子图片

    1、地址

    http://www.jder.net/meizi/

    代码:

    #!/usr/bin/python3.6  
    # -*- coding: utf-8 -*- 
    
    import requests
    from bs4 import BeautifulSoup
    import codecs
    import time
    import os.path
    
    # 伪装成浏览器进行登录
    
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"}
    def download_page(url):
       r = requests.get(url, headers=headers)  # 增加headers, 模拟浏览器
       return  r.text
    
    def download_pic(imgUrl):
      r = requests.get(imgUrl,headers=headers)
      return r.content
    
    def getFilePath(title,link):
      pic =  './/pic'
      if not os.path.exists(pic):
        os.makedirs(pic)
        pass
      return pic + '/{}.{}'.format(title,link.split('.')[-1])
    
    def get_content(html):
       soup = BeautifulSoup(html, 'html.parser')
       main_ll = soup.find_all('div',class_='picture-box')
       for ll in main_ll:
        a_tag = ll.find(class_='picture-img').find('a')
        img = a_tag.find('img')
    
        link = img.get('src')
        start = link.find('=') + 1
        end = link.find('&')
        realLink = link[start:end]
    
        title = img.get('alt')
        r = download_pic(realLink)
    
        filepath = getFilePath(title,realLink)
    
        with open(filepath,'wb') as f:
          f.write(r)
          time.sleep(1)
    
    
    def main():
           url = 'http://www.jder.net/meizi/'
           html = download_page(url)
           get_content(html)
    
    
    if __name__ == '__main__':
       main()

    附:

    BeautifulSoup 使用文档:

    https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/#

  • 相关阅读:
    sql.srcipt
    sowmodaldialog
    4) 删除虚拟应用程序
    JavaScript读写Cookies
    第5章 脚本运行期库对象
    npm serve md 工具 [MD]
    cleanmark 清除格式 博客内容提取 [MD]
    Hex编码 十六进制编码
    Windows Server AppFabric(Codename:"Dublin&Velocity")介绍
    WF4设计器模型:编辑范围ModelEditingScope
  • 原文地址:https://www.cnblogs.com/roger-jc/p/11981138.html
Copyright © 2011-2022 走看看