#coding=utf-8
import urllib
import re

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

def getImg(html):
    reg = r'src="(.+?\.jpg)" pic_ext'
    imgre = re.compile(reg)
    imglist = re.findall(imgre,html)
    x = 0
    for imgurl in imglist:
        urllib.urlretrieve(imgurl,'%s.jpg' % x)
        x+=1


html = getHtml("http://tieba.baidu.com/p/2460150866")

print getImg(html)

  

    re.compile() 可以把正则表达式编译成一个正则表达式对象.

  re.findall() 方法读取html 中包含 imgre(正则表达式)的数据。

  这里的核心是用到了urllib.urlretrieve()方法,直接将远程数据下载到本地。

 

相关文章:

  • 2021-07-15
  • 2021-07-08
  • 2021-09-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
  • 2021-05-19
  • 2021-10-21
猜你喜欢
  • 2022-12-23
  • 2021-04-05
  • 2021-08-11
  • 2021-12-22
  • 2021-10-01
  • 2021-04-23
相关资源
相似解决方案