第一个爬虫的程序
参考链接:https://www.cnblogs.com/fnng/p/3576154.html
下面是我稍加改动后的代码,试了好几个都成功了
#coding=utf-8
import urllib.request
import re
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read() #读取url上的数据,并把整个页面下载下来
return html
def getImg(html):
reg = r'class="BDE_Image" src="(.+?\.jpg)"'
imgre = re.compile(reg)
imglist = re.findall(imgre,html.decode('utf-8'))
x=0
for imgurl in imglist:
urllib.request.urlretrieve(imgurl, '%s.jpg' %x)
x+= 1
return imglist
html = getHtml("http://tieba.baidu.com/p/6565885468")
print(getImg(html))
print(html)