担心人人网会黄掉,写个爬虫,把我的相册照片都下载下来。代码如下:

# -*- coding: utf-8 -*-
import requests
import json
import os def mkdir(path):
path=path.strip()
path=path.rstrip("\\")
isExists=os.path.exists(path)
if not isExists:
print path+u' 创建成功'
os.makedirs(path)
return "yes"
else:
print path+u' 目录已存在'
return "no" def login_renren(s):
origin_url = 'http://www.renren.com'
login_data = {
'email':'用户名',
'domain':'renren.com',
'origURL':'http://www.renren.com/home',
'key_id':'',
'captcha_type':'web_login',
'password':'密码抓包获得',
'rkey':'rkey抓包获得'
}
r = s.post("http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2016742045262", data = login_data)
if 'true' in r.content:
print u'登录人人网成功'
return s
def get_albums(s):
r = s.get('http://photo.renren.com/photo/278382090/albumlist/v7?showAll=1#')
#print r.content
content = r.content
index1 = content.find('nx.data.photo = ')
#print index1
index2 = content.find('nx.data.hasHiddenAlbum =')
#print index2
target_json = content[index1+16:index2].strip()
target_json = target_json[0:len(target_json)-1]
#print target_json
data = json.loads(target_json.replace("\'", '"'));
album_list = data['albumList']
album_count = album_list['albumCount']
tip = u'一共有'+str(album_count)+u'个相册'
print tip
album_ids = []
for album in album_list['albumList']:
#print album['albumName']
album_ids.append(album['albumId'])
return album_ids,s def download_albums(album_ids,s):
#访问相册
for album_id in album_ids:
album_url = 'http://photo.renren.com/photo/278382090/album-'+album_id+'/v7'
r = s.get(album_url)
if "photoId" in r.content:
print u'进入相册成功'
#print r.content
content = r.content
index1 = content.find('nx.data.photo = ')
#print index1
index2 = content.find('; define.config')
#print index2
target_json = content[index1+16:index2].strip()
target_json = target_json[13:len(target_json)-2]
#print target_json
data = json.loads(target_json.replace("\'", '"'));
photos = data['photoList']
album_name = data['albumName']
# 定义并创建目录
album_path = 'd:\\'+album_name
#print album_path
if mkdir(album_path)=='yes':
for photo in photos:
#print photo['url']
image_name = photo['photoId']
photo_url = photo['url']
r = requests.get(photo_url)
image_path = album_path+'/'+image_name+'.jpg'
f = open(image_path, 'wb')
f.write(r.content)
f.close()
tip = u'相片'+image_name+u'下载成功'
print tip
else:
print u'相册已经下载' #执行该文件的主过程
if __name__ == '__main__':
#创建requests会话
s = requests.Session()
#登录人人网
s = login_renren(s)
#获取相册列表
album_ids,s = get_albums(s)
#下载相册
download_albums(album_ids,s)

搞定!运行效果如下:

用python requests库写一个人人网相册爬虫的更多相关文章

  1. 使用python requests库写接口自动化测试--记录学习过程中遇到的坑(1)

    一直听说python requests库对于接口自动化测试特别合适,但由于自身代码基础薄弱,一直没有实践: 这次赶上公司项目需要,同事小伙伴们一起学习写接口自动化脚本,听起来特别给力,赶紧实践一把: ...

  2. 大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。

    python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url ...

  3. 用python的time库写一个进度条

    运算符 算数运算 如a=10,b=20 +两个数相加 a+b=30 -两个数相减 a-b=-10 两个数相乘 a****b =200 /两个数相除b/a=2 %取模,并返回余数b%a=0 幂,a*** ...

  4. 【python爬虫】用requests库模拟登陆人人网

    说明:以前是selenium登陆取cookie的方法比较复杂,改用这个 """ 用requests库模拟登陆人人网 """ import r ...

  5. Python Requests库简单入门

    我对Python网络爬虫的学习主要是基于中国慕课网上嵩天老师的讲授,写博客的目的是为了更好触类旁通,并且作为学习笔记之后复习回顾. 1.引言 requests 库是一个简洁且简单的处理HTTP请求的第 ...

  6. python requests库学习笔记(上)

    尊重博客园原创精神,请勿转载! requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.pytho ...

  7. Python——Requests库的开发者接口

    本文介绍 Python Requests 库的开发者接口,主要内容包括: 目录 一.主要接口 1. requests.request() 2. requests.head().get().post() ...

  8. Python:requests库、BeautifulSoup4库的基本使用(实现简单的网络爬虫)

    Python:requests库.BeautifulSoup4库的基本使用(实现简单的网络爬虫) 一.requests库的基本使用 requests是python语言编写的简单易用的HTTP库,使用起 ...

  9. Python requests库的使用(一)

    requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.python-requests.org/z ...

随机推荐

  1. [原创] 关于quartz (spring 中的任务调度器)时间配置

    1.   CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 ...

  2. C#: 集合

    摘自http://www.cnblogs.com/kissdodog/archive/2013/01/29/2882195.html 先来了解下集合的基本信息 1.BCL中集合类型分为泛型集合与非泛型 ...

  3. oracle 复杂语句

    select nvl(sum1,'0')as sum1,nvl(sum2,'0') as sum2,da2 from( select count(*) as sum1,substr(APPLY_DAT ...

  4. c++ 中this底层

    成员变量设置在一个结构体中, 操作成员变量的成员函数,其实质上就是拥有一个隐藏的 成员变量结构体的地址指针,俗称this指针.

  5. HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 Problem Description The soil is cracking up beca ...

  6. [转]ms sql 2000 下批量 附加/分离 数据库(sql语句)

    这次公司要把MS SQL Server 2000 服务器上的数据库复制到新的服务器上面去,于是几百个数据库文件就交给我附加到新服务器上了   以前一直没接触过这方面的东西,于是果断谷歌了也百度了  找 ...

  7. 夺命雷公狗ThinkPHP项目之----企业网站23之网站前台二级分类的跳转(URL跳转到列表页或产品页)

    我们现在开始做实现我们的二级菜单如何跳转到指定的列表页或者产品也呢?? 我们分享下数据库情况: 我们的数据库里提前给我们预留了一个cate_type的字段,那么我们可以让这个字段进行判断,从而遍历出指 ...

  8. clock divider

    一个clock的产生: 1) Clock source的选择: cgm_mux5(.clk_out, .clk_in0, .clk_in1, .clk_in2, .clk_in3, .clk_in4, ...

  9. jquery tab键转换

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. Sql Server 2012 的新分页方法分析(offset and fetch) - 转载

    最近在分析 Sql Server 2012 中 offset and fetch 的新特性,发现 offset and fetch 无论语法的简洁还是功能的强大,都是相当相当不错的 其中 offset ...