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

    from urllib.request import urlopen, urlretrieve
    import re
    # 大部分网址都可以直击换网址
    url = "http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=%E9%BE%99%E5%8D%B7%E9%A3%8E"
    html = urlopen(url)
    obj = html.read().decode() # 得到网页HTML源码
    print(obj)
    urls = re.findall(r'"objURL":"(.*?)"', obj) # 在这一步,获取网页中的objURL部分,也就是真正的图片地址
    index = 0
    for u in urls:
    if index <= 1: # 控制下载10张
    try:
    print('Downloading...%d' % (index))
    urlretrieve(u, 'pic' + str(index) + '.png') # urlretrieve函数 下载图片
    index += 1
    except Exception: # 当由于网络原因或图片服务器出现问题时,捕获异常即可,不使程序退出
    print('Downloading Failed%d' % (index))
    finally:
    print('Downloading Complete')
    else:
    break
  • 相关阅读:
    Python操作Mongo数据库
    Python正则模块
    Python时间模块
    Python协程(下)
    Python协程(中)
    Python协程(上)
    aiohttp
    常用排序算法的Python实现
    江苏省各地级市58同城租房数据
    百合网
  • 原文地址:https://www.cnblogs.com/dujunjie/p/12066126.html
Copyright © 2011-2022 走看看