1.爬网页源码
#coding=utf-8'
import urllib2
def getHtml(url):
page=urllib2.urlopen(url)
html=page.read()
return html
html=getHtml("https://www.zhihu.com/")
print html.decode('utf-8')
2.爬图片
#coding=utf-8
import urllib
import re
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def getImg(html):
reg = 'src="(http.*.jpg)"'
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
x = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl,'/home/xuhang/桌面/new/test/%s.jpg' % x)
x+=1
html = getHtml("http://tieba.baidu.com/p/4004627057")
print getImg(html)