python爬虫抓取网站图片并保存

嘿嘿,这个有点邪恶了,前段时间在OSC上看到的,使用BeautifulSoup抓取豆瓣妹子的图片并保存,但是那个程序在我电脑上运行总是出错,所以我就想着自己写一个,今天下午终于闲下来然后实现了,好了,废话不说,下面就贴出程序

import urllib2
from bs4 import BeautifulSoup
import socket

baseurl = "http://dbmeizi.com/"
#伪装浏览器,以免被封
def user_agent(url):
    req_header = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
    req_timeout = 20
    try:
        req = urllib2.Request(url,None,req_header)
        page = urllib2.urlopen(req,None,req_timeout)
        html = page
    except urllib2.URLError as e:
        print e.message
    except socket.timeout as e:
        user_agent(url)
    return html

def page_loop(pageid):
    url = baseurl+'?p=%s'%pageid
    print url
    page = user_agent(url)
    soup = BeautifulSoup(page)
    total_img = 0
    img = soup.find_all(['img'])
    for myimg in img:
        link = myimg.get('src')
        total_img += 1
        print link
      #  content2 = urllib2.urlopen(link).read()
        content2 = user_agent(link).read()
        #这句代码直接从OSC上面弄下来的
        #D:\myimg是保存路径,你可以自己改成自己的,但是路径必须要自己创建好
        with open(u'D:\myimg'+'/'+link[-11:],'wb') as code:
            code.write(content2)
    print total_img
    return total_img
page_start = 0
page_stop = 4
total = 0
for i in range(page_start,page_stop):
    total+=page_loop(i)

print total
#total就是统计下总共保存到本地的图片数量

上面的page_start和page_stop就是抓取的起始页和终止页,我自己看了下大概到400页的,然后我在我服务器上运行这个程序,总共下了7944张,哈哈,不过不大,总共就128M…….

好了,程序就是这样,大家自己看着办吧,haha

发表评论

电子邮件地址不会被公开。 必填项已用*标注