Python爬蟲初試

勿在浮沙築高臺LS發表於2017-02-09
#coding=utf-8
import urllib.request
import re

def getHtml(url):
    page = urllib.request.urlopen(url)
    html = page.read()
    html = html.decode('utf-8')
    return html

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


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

getImg(html)

相關文章