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

# -*- 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. c语言的基本语法

    1. 二目运算符从右往左 优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右   () 圆括号 (表达式)/函数名(形参表)   . 成员选择(对象 ...

  2. Leetcode: Count Numbers with Unique Digits

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  3. Lintcode: Interval Minimum Number

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  4. 2-sat 输出任意一组可行解&拓扑排序+缩点 poj3683

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8170   Accept ...

  5. struts文件上传(单文件)

    第01步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  6. java 虚拟机工具

    jps 命令 可以查询开启了rmi服务的远程虚拟机进程状态. -v jvm参数. jstat -gcutil命令 [cangyue@/System/Library/Frameworks/JavaVM. ...

  7. python入门语法总结 zz

    http://renjie120.iteye.com/blog/680126 1.python是一个解释性语言: 一个用编译性语言比如C或C++写的程序可以从源文件(即C或C++语言)转换到一个你的计 ...

  8. HTML5的自定义属性data-* 的用法解析

    人们总喜欢往HTML标签上添加自定义属性来存储和操作数据.但这样做的问题是,你不知道将来会不会有其它脚本把你的自定义属性给重置掉,此外,你这样做也会导致html语法上不符合Html规范,以及一些其它副 ...

  9. php基础知识和函数

    <?php /* echo "hello","aaaa"; //输出语法,可以输出多个字符串 print "world"; //可以输 ...

  10. 搞笑的u盘图片