#coding=utf-8
from bs4 import BeautifulSoup
import urllib.request
import re
def gethtml(url):
page=urllib.request.urlopen(url).read()
html=page.decode('utf-8')#中文转码
return html
def getimage(html):
reg=r'src="(.+?\.jpg)" pic_ext'
imgre=re.compile(reg)
imagelist=imgre.findall(html)
#imagelist=re.findall('src="(.+?\.jpg)" pic_ext',html,re.M)
return imagelist
def download(imagelist,local):
x=0
for i in imagelist:
urllib.request.urlretrieve(i,local+'%s.jpg'%x)
x+=1
if __name__=='__main__':
url='http://tieba.baidu.com/p/2460150866'
html=gethtml(url)
pic=getimage(html)
local='C:\\downloadpicture\\'
#download(pic,local)
这个算是第一个入门爬虫的例子了,是模仿的教程 一点一点拼出来 实现的是爬去页面中的图片并下载下来