python 豆瓣图片的爬取

   豆瓣图片的抓取:在python中实现生产者和消费者模型的实现,大家可以参考这篇文章 http://www.bkjia.com/Pythonjc/978391.html

个人认为是讲的比较易懂的,只要看看仿写几个例子,感觉这一块就差不多了。下面的代码并没有抓取豆瓣相册的全部,这是找了一个推荐较多的抓取来玩玩,也只抓取前面20页,每页有30张图片,所以可以根据这个去跟新url。维护了一个list来保存图片的url,一个消费者函数来下载图片,一个生产者函数来取图片的url , 下面看代码:

# _*_coding:utf-8_*_

import urllib2
import cookielib
from bs4 import BeautifulSoup
import re
import time
import threading

start = start_time = time.ctime()
s = []
max_length = 30
condition = threading.Condition()


class Producer(threading.Thread):
    def run(self):
        for i in xrange(20):
            condition.acquire()
            if len(s) == max_length:
                print 's is full'
                condition.wait()
            request_url = 'https://site.douban.com/widget/public_album/86320/?start=%s' % (i*30) #推荐相册的url

            headers = {
                    'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36',
                }

            opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
            response = urllib2.Request(request_url, headers=headers)
            html = opener.open(response)
            soup = BeautifulSoup(html)
            img_urls = soup.find_all('a', {'class': 'album_photo'})

            for item in img_urls:
                p = re.compile(r'src="(.*?)"')
                img_url = p.search(str(item.find('img'))).group(1)  #图片的url
                s.append(img_url)
                print 'producer somthing'
            condition.notify()


class Consumer(threading.Thread):
    def run(self):
        count = 0
        while True:
            if condition.acquire():
                if not s:
                    print 's is empty wait'
                    condition.wait()
                img_url = s.pop(0)
                print 'consumer something'
                with open('E:\\douban\\%s.jpg' %count, 'wb') as fp:
                    try:
                        response_img = urllib2.urlopen(img_url).read() #下载图片
                        fp.write(response_img)
                    except Exception:
                        print 'error'
                count += 1
                condition.notify()
                condition.release()

t1 = Producer()
c1 = Consumer()
t1.start()
c1.start()

嗯, 差不多就这样,大家还可以多开几个线程,使得下载速度更快点,毕竟如果抓取大量的图片的话,io操作会比较耗时。

这是图片的部分截图:

 

 

转载于:https://www.cnblogs.com/viver-python/p/6064620.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值