爬虫入门:爬取百度贴吧图片

#coding = utf-8

importre

importurllib

def getHtml(url):

page = urllib.urlopen(url)

html = page.read()

return html

def getImg(html):

reg =r'src="(.+?\.jpg)" pic_ext'

#reg = r'src="(.+?\.jpg)" alt'

imgre = re.compile(reg)

imglist = re.findall(imgre,html)

x=1

for imgurl in imglist:

urllib.urlretrieve(imgurl,'C:\\img\\%s.jpg'% x)

x+=1

return x

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

print getImg(html)


执行结果一共69张图片:


爬虫入门:爬取百度贴吧图片_第1张图片

你可能感兴趣的:(爬虫入门:爬取百度贴吧图片)