chengyuyu
 

刚开始学python3,百度了下python的用法,看到爬虫下载图片很好玩,于是百度各种资料学习了下,结果总是运行不成功,最后终于改好了,记录下。

from urllib import request
import urllib
import re

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

def getImg(html):
    reg = r\'src="(.+?\.jpg)"\'
    imgre = re.compile(reg)
    html=html.decode(\'utf-8\')#不写此行会报错:TypeError: cannot use a string pattern on a bytes-like object
    imglist = re.findall(imgre,html)
    x = 1
    for imgurl in imglist:
        urllib.request.urlretrieve(imgurl,\'C:/Users/zx/Desktop/images/%s.jpg\' % x)
        x+=1
    return imglist

html = getHtml("http://www.win4000.com/wallpaper_2358_0_10_1.html")
print(getImg(html))

标红的是python3和python2的不同之处,如不写则会报错:AttributeError: module \'urllib\' has no attribute \'urlopen\'

如果返回[ ],可能是正则表达式不对

分类:

技术点:

相关文章:

  • 2021-11-30
  • 2021-11-26
  • 2022-12-23
  • 2021-10-25
  • 2022-12-23
  • 2021-05-25
  • 2021-12-08
  • 2021-06-13
猜你喜欢
  • 2021-12-10
  • 2021-11-30
  • 2021-09-04
  • 2021-11-30
  • 2021-11-30
  • 2022-12-23
相关资源
相似解决方案