用python requests库写一个人人网相册爬虫
担心人人网会黄掉,写个爬虫,把我的相册照片都下载下来。代码如下:
# -*- 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库写一个人人网相册爬虫的更多相关文章
- 使用python requests库写接口自动化测试--记录学习过程中遇到的坑(1)
一直听说python requests库对于接口自动化测试特别合适,但由于自身代码基础薄弱,一直没有实践: 这次赶上公司项目需要,同事小伙伴们一起学习写接口自动化脚本,听起来特别给力,赶紧实践一把: ...
- 大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。
python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url ...
- 用python的time库写一个进度条
运算符 算数运算 如a=10,b=20 +两个数相加 a+b=30 -两个数相减 a-b=-10 两个数相乘 a****b =200 /两个数相除b/a=2 %取模,并返回余数b%a=0 幂,a*** ...
- 【python爬虫】用requests库模拟登陆人人网
说明:以前是selenium登陆取cookie的方法比较复杂,改用这个 """ 用requests库模拟登陆人人网 """ import r ...
- Python Requests库简单入门
我对Python网络爬虫的学习主要是基于中国慕课网上嵩天老师的讲授,写博客的目的是为了更好触类旁通,并且作为学习笔记之后复习回顾. 1.引言 requests 库是一个简洁且简单的处理HTTP请求的第 ...
- python requests库学习笔记(上)
尊重博客园原创精神,请勿转载! requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.pytho ...
- Python——Requests库的开发者接口
本文介绍 Python Requests 库的开发者接口,主要内容包括: 目录 一.主要接口 1. requests.request() 2. requests.head().get().post() ...
- Python:requests库、BeautifulSoup4库的基本使用(实现简单的网络爬虫)
Python:requests库.BeautifulSoup4库的基本使用(实现简单的网络爬虫) 一.requests库的基本使用 requests是python语言编写的简单易用的HTTP库,使用起 ...
- Python requests库的使用(一)
requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.python-requests.org/z ...
随机推荐
- BZOJ1930 [Shoi2003]pacman 吃豆豆
dp,首先建出图,f[i][j]表示a吃到了i点,b吃到了j点的最大值,转移的时候转移拓扑序小的那一维,如果i拓扑序小于j,那么转移到f[k][j],否则转移到f[i][k],建出的图边数也要优化, ...
- springmvc处理ajax请求
1.controller将数据封装成json格式返回页面 @RequestMapping("/dataList") public void datalist(CsoftCunsto ...
- 组合逻辑的Glitch与时序逻辑的亚稳态
竞争(Race):一个门的输入有两个及以上的变量发生变化时,由于各个输入的组合路径的延时不同,使得在门级输入的状态改变非同时. 冒险或险象(Hazard):竞争的结果,如毛刺Glitch. 相邻信号间 ...
- HTML5,超级链接
<a href="http://h123.date">预算控制系统</a><<br><a href="2.html&quo ...
- 锋利的JQuery(五)
jQuery与Ajax: load: load(url) $("#resText").load("test.html") 加载所有元素 load(url ...
- Openstack的HA解决方案【haproxy和keepalived】
1. 安装haproxy,keepalived, httpd,3台机器一致. yum install haproxy keepalived httpd -y 2. 修改httpd的默认页面. 在/va ...
- 不允许调用库函数,也不允许使用任何全局或局部变量编写strlen函数
不允许调用库函数,也不允许使用任何全局或局部变量编写strlen函数. 这是一道面试题,可以使用递归的方式解答,答案如下: #include <stdio.h> int mylen(cha ...
- 关于 MySQL LEFT JOIN 你可能需要了解的三点
即使你认为自己已对 MySQL 的 LEFT JOIN 理解深刻,但我敢打赌,这篇文章肯定能让你学会点东西! ON 子句与 WHERE 子句的不同 一种更好地理解带有 WHERE ... IS NUL ...
- 将edit ctrL弄的像个dos
case WM_CTLCOLOREDIT: { HWND hShellText = GetDlgItem(hDlg,IDC_TXT_SHELL); if (hShellText == (HWND)lP ...
- va_list/va_start/va_arg/va_end深入分析【转】
转自:http://www.cnblogs.com/justinzhang/archive/2011/09/29/2195969.html va_list/va_start/va_arg/va_end ...