python3 爬虫 简单示例

#!/usr/local/python3/bin/python3
#coding:UTF-8
import urllib
import urllib.request#python3 later ...
import re

def getHtml(url):
	page = urllib.request.urlopen(url)#python later ...
	html = page.read()
	return html

def getImg(html):
	reg = r'src="(.+?\.jpg)" pic_ext'
	imgre = re.compile(reg)
	html=html.decode('utf-8')#python3 later...
	imglist = re.findall(imgre,html)
	x = 0
	for imgurl in imglist:
		urllib.request.urlretrieve(imgurl,'%s.jpg' % x)#python3 later ...
		x+=1

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

print(getImg(html))

posted @ 2024-11-03 11:33  Maguyusi  阅读(11)  评论(0)    收藏  举报  来源