首页 > 其他 > 详细

爬虫 spider

时间:2017-05-07 21:36:31      阅读:271      评论:0      收藏:0      [点我收藏+]

python 2.x

# -*- coding: utf-8 -*-
import re
import urllib

url = ‘http://tieba.baidu.com/p/4872795764‘
page = urllib.urlopen(url)
html = page.read()

r = ‘src="(.*?\.jpg)" size‘ # 注意此处?的作用,取消贪婪匹配 结合findall方法,只匹配分组中的内容
imgre = re.compile(r)
imglist = re.findall(imgre, html)

count = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl, filename=‘mac_book Pro %s.jpg‘ % count)
count += 1




函数:

def gethtml(url):
html = urllib.urlopen(url).read()
return html

def getimg(html):
r = ‘src="(.*?\.jpg)" size‘
imgre = re.compile(r)
imglist = re.findall(imgre, html)
print imglist
count = 0
for imgurl in imglist:
urllib.urlretrieve(imgurl, filename=‘mac_book_Pro_%s.jpg‘ % count)
count += 1
html = gethtml(‘http://tieba.baidu.com/p/4872795764‘)
getimg(html)

爬虫 spider

原文:http://www.cnblogs.com/yum777/p/6822379.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!