python 3.4 所写爬虫
仅仅是个demo,已百度图片首页图片为例。能跑出图片上的图片;
使用 eclipse pydev 编写:
from SpiderSimple.HtmLHelper import *
import imp
import sys
imp.reload(sys)
#sys.setdefaultencoding('utf-8')
html = getHtml('http://image.baidu.com/')
try:
getImage(html)
exit()
except Exception as e:
print(e)
HtmlHelper.py文件
上面的 SpiderSimple是自定义的包名
from urllib.request import urlopen,urlretrieve
#正则库
import re
#打开网页
def getHtml(url):
page = urlopen(url)
html = page.read()
return html
#用正则爬里面的图片地址
def getImage(Html):
try:
#reg = r'src="(.+?\.jpg)" class'
#image = re.compile(reg)
image = re.compile(r'<img[^>]*src[=\"\']+([^\"\']*)[\