python爬虫抓取图片
2013-07-03 14:54
465 查看
关于python爬虫一直以来是很著名的,林林总总也有很多方法,大致起来也就是一个原理。
下面我来介绍一下我用的BeautifulSoup获取的,正则获取也很简单,在这里只说一下BeautifulSoup方法,使用伯乐在线网站作为参考的例子
代码如下
为了提高效率我使用的多线程的方法获取图片,使用啦urllib中的urlretrieve来下载图片,其中urlretrieve的参数是这样的:
urlretrieve(url,filename)
url:图片的网络路径
filename:图片的本地路径
下面我来介绍一下我用的BeautifulSoup获取的,正则获取也很简单,在这里只说一下BeautifulSoup方法,使用伯乐在线网站作为参考的例子
代码如下
#encoding:UTF-8 import urllib2,urllib,random,threading from bs4 import BeautifulSoup import sys reload(sys) sys.setdefaultencoding('utf-8') class Images(threading.Thread): def __init__(self,lock,src): threading.Thread.__init__(self) self.lock=lock self.src=src def run(self): self.lock.acquire() urllib.urlretrieve(self.src,'/home/tron/Python/code/img/'+str(random.choice(range(9999)))) print self.src+"已获取" self.lock.release() def img_greb(): lock=threading.Lock() site_url = "http://blog.jobbole.com/" html = urllib2.urlopen(site_url).read() soup=BeautifulSoup(html) img=soup.findAll(['img']) for i in img: Images(lock,i.get('src')).start() if __name__ == '__main__': img_greb()
为了提高效率我使用的多线程的方法获取图片,使用啦urllib中的urlretrieve来下载图片,其中urlretrieve的参数是这样的:
urlretrieve(url,filename)
url:图片的网络路径
filename:图片的本地路径
相关文章推荐
- 第一个python程序,小爬虫--抓取网页图片
- Python爬虫之一 PySpider 抓取淘宝MM的个人信息和图片
- Python爬虫抓取网页图片
- Python爬虫抓取虎扑论坛帖子图片
- python爬虫——BeautifulSoup 抓取图片
- Python爬虫抓取糗百的图片,并存储在本地文件夹
- Python3爬虫抓取TP官网案例图片
- Python爬虫学习笔记二:百度贴吧网页图片抓取
- Python爬虫抓取网页图片
- python爬虫 抓取图片入门
- 爬虫案例---Python2X版本抓取京东手机页面的图片
- Python爬虫---------------<妹子图>图片抓取(1)
- python爬虫之抓取网页中的图片到本地
- python实现简单爬虫抓取图片
- 用python爬虫抓取知乎图片
- Python爬虫实战(1)——百度贴吧抓取帖子并保存内容和图片
- Python 爬虫抓取图片(分页)
- python爬虫抓取图片到本地
- Python爬虫--抓取百度贴吧图片
- Python爬虫之知乎图片抓取