zoukankan      html  css  js  c++  java
  • Python3爬虫获取

      “AttributeError: 'module' object has no attribute 'urlopen'”
     
    原因是Python3里的urllib模块已经发生改变,此处的urllib都应该改成urllib.request。
     
    修改之后再运行,发现又有如下提示:
     
        TypeError: can't use a string pattern on a bytes-like object
     
    原因为Python3 findall数据类型用bytes类型,因此在正则表达式前应添加html = html.decode('utf-8')。
     
    修改完后运行,成功~,不过由于网站原因,仍只能保存最近的24张背景图。最终代码如下:

    #!/usr/bin/env python

    # -*- coding:utf-8 -*-

    # -*- author:arron ni-*-

    # python3抓取bing主页所有背景图片

    import urllib,re,sys,os

    def get_bing_backphoto():

        if (os.path.exists('photos')== False):

            os.mkdir('photos')

        for i in range(0,30):

            url = 'http://cn.bing.com/HPImageArchive.aspx?format=js&idx='+str(i)+'&n=1&nc=1361089515117&FORM=HYLH1'

            html = urllib.request.urlopen(url).read()

            if html == 'null':

                print( 'open & read bing error!')

                sys.exit(-1)

            html = html.decode('utf-8')

            reg = re.compile('"url":"(.*?)","urlbase"',re.S)

            text = re.findall(reg,html)

            #http://s.cn.bing.net/az/hprichbg/rb/LongJi_ZH-CN8658435963_1366x768.jpg

            for imgurl in text :

                right = imgurl.rindex('/')

                name = imgurl.replace(imgurl[:right+1],'')

                savepath = 'photos/'+ name

                urllib.request.urlretrieve(imgurl, savepath)

                print (name + ' save success!')

    get_bing_backphoto()

     

  • 相关阅读:
    异步加载text资源,加载一次、执行一次、链式回调
    贝叶斯判断类别
    通过贝叶斯概率机器学习
    什么是 Dropout
    什么是CNN--Convolutional Neural Networks
    神经网络介绍
    神经网络之线性单元
    机器学习十大常用算法
    对比学习用 Keras 搭建 CNN RNN 等常用神经网络
    机器学习,人工智能相关最新图书推荐
  • 原文地址:https://www.cnblogs.com/zfquan/p/8057672.html
Copyright © 2011-2022 走看看