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

    import urllib.request
    import re
    import os


    def getHtml(url):
    page = urllib.request.urlopen(url)
    html = page.read()
    return html


    def getImg(html):
    imglist = re.findall('img src="(http.*?)"', html)
    # 1 #http.*?表示非贪婪模式的匹配,只要符合http就匹配完成,不再看后面的内容是否匹配,即在能使整个匹配成功的前提下,使用最少的重复
    return imglist


    html = getHtml("https://www.zhihu.com/question/39731953").decode("utf-8")
    imagesUrl = getImg(html)

    if os.path.exists("E:/imags") == False:
    os.mkdir("E:/imags")

    count = 0 # 文件的起始名称为 0
    for url in imagesUrl:
    print(url)
    if (url.find('.') != -1): # 2
    name = url[url.find('.', len(url) - 5):]
    bytes = urllib.request.urlopen(url)
    f = open("E:/imags/" + str(count) + name, 'wb')# 代开一个文件,准备以二进制写入文件
    f.write(bytes.read()) # write并不是直接将数据写入文件,而是先写入内存中特定的缓冲区
    f.flush() # 将缓冲区的数据立即写入缓冲区,并清空缓冲区
    f.close() # 关闭文件
    count += 1
  • 相关阅读:
    ES6学习之装饰器
    ES6学习之Class
    ES6学习之Async函数
    ES6学习之Generator函数
    for循环及break和continue的区别
    ES6学习之Iterator和For...of循环
    js检测对象属性
    ES6学习之Promise
    ES6学习之Reflect
    Visual Studio references中的package找不到
  • 原文地址:https://www.cnblogs.com/1510152012huang/p/8646452.html
Copyright © 2011-2022 走看看