以下是抓取网站美女图片的Python爬虫程序实现代码,注:需安装bs4 模块,以下是实现代码


# -*- coding:utf8 -*-
from bs4 import BeautifulSoup
import os, sys, urllib2,time,random

path = os.getcwd()
new_path = os.path.join(path,u'download')
if not os.path.isdir(new_path):
os.mkdir(new_path)

def page_loop(page=1):
url = 'http://me2-sex.lofter.com/?page=%s' % page
print url
content = urllib2.urlopen(url)
soup = BeautifulSoup(content)
my_girl = soup.find_all('img')
for girl in my_girl:
link = girl.get('src')
flink = link
print flink
content2 = urllib2.urlopen(flink).read()

#with open(u'loft'+'/'+time.strftime('%H-%M-%S')+random.choice('qwertyuiopasdfghjklzxcvbnm')+flink[-5:],'wb') as code:
with open(u'download'+'/'+flink[-11:],'wb') as code:
code.write(content2)

page = int(page) + 1
print u'开始抓取下一页'
print 'the %s page' % page
page_loop(page)

page_loop()
print "~~~~~~~~~~~~~~~~~~~~~~~~~~END~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
raw_input("Press <Enter> To Quit!")

以下是支行效果图:
111

222


转载本站文章请注明,转载自:伟哥的技术博客 https://www.wuxiaowei.com

本文永久链接: https://www.wuxiaowei.com/program/python/%e4%b8%80%e4%b8%aa%e6%8a%93%e5%8f%96%e7%bd%91%e7%ab%99%e7%be%8e%e5%a5%b3%e5%9b%be%e7%89%87%e7%9a%84python%e7%88%ac%e8%99%ab%e7%a8%8b%e5%ba%8f/