zoukankan      html  css  js  c++  java
  • python 爬取图片

    使用python的requests库爬取网页时,获取文本一般使用text方法,如果要获取图片并保存要用content

    举个栗子,爬煎蛋网的图:

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    import requests
    import re
    import os
    
    url="http://jandan.net/ooxx"
    s = requests.session()
    header_jandan={'Host': 'jandan.net',
            'Connection': 'keep-alive',
            'Cache-Control': 'max-age=0',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Referer': 'http://jandan.net/ooxx',
            'Accept-Encoding': 'gzip, deflate, sdch',
            'Accept-Language':'zh-CN,zh;q=0.8'}
    resp = s.get(url,headers=header_jandan,timeout=10)
    if len(resp.text) < 1500:
        resp2 = s.get(url,headers=header_jandan,timeout=10)
        text=resp2.text
    else:
        text=resp.text
    #print rn.text
    img_url=re.findall(ur'(?<=<img src=").*?(?=")',text)
    d=os.getcwd()
    for i in img_url:
        ret=i.split("/")
        file = ret[-1]
        #print file
        if i.find("http") == -1:
            url_img="http:"+i
            r_img=s.get(url_img,headers=header_jandan,timeout=10)
            open(os.path.join(d,file), 'wb+').write(r_img.content)
            print "write %s" % file

    考虑到如果图片很大,获取需要时间,设置timeout超时避免内容取不完整。

    写文件内容为r_img.content

    打开文件的方式使用wb+,二进制文件覆盖方式写入。

  • 相关阅读:
    根据dateFormatter创建NSDate类型数据
    centos6.5下oracle自动备份删除指定天数的文件
    svg-edit和svg中的自定义属性
    vc读取当前路径和读取配置ini文件
    powerdesiner技巧
    oracle理解和导入导出
    highstock无图像
    winform中datagridview刷新后的排序记忆
    freemarker取数
    winform clickonce在线安装
  • 原文地址:https://www.cnblogs.com/taurusfy/p/7158801.html
Copyright © 2011-2022 走看看