python 模拟登录web 库_python 模拟Web登录与下载

import httplib2

import urllib

import os

from urllib import urlencode

# config

m_downurl = 'http://dl.iteye.com/topics/download/46527af3-941f-38f0-b0f3-9f74bf5ffa67'

m_host = "http://www.iteye.com/login"

m_username_value = "***********"

m_password_value = "***********"

m_authenticity_token = "*************"

# create http cache

h = httplib2.Http('.cache')

# open debug

httplib2.debuglevel = 1

# get login url

getheader = {"Accept":"text/html, application/xhtml+xml, */*",

"Accept-Language":"zh-CN",

"User-Agent":"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",

"Accept-Encoding":"gzip, deflate",

"Host":"www.iteye.com",

"Connection": "Keep-Alive",

"cache-control":"no-cache"

}

response1, content1 = h.request(m_host, headers = getheader)

data = {'authenticity_token': m_authenticity_token, 'name' : m_username_value, 'password': m_password_value , 'button': '%E7%99%BB%E3%80%80%E5%BD%95'}

postheader = {"Accept":"text/html, application/xhtml+xml, */*",

"Referer": "http://www.iteye.com/login",

"Accept-Language":"zh-CN",

"User-Agent":"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",

"Accept-Encoding":"gzip, deflate",

"Host":"www.iteye.com",

"Connection": "Keep-Alive",

"cache-control":"no-cache",

'Cookie': response1['set-cookie']

}

# authentication supports SSL | Http Basic

# h.add_credentials(m_username_value, m_password_value )

# login

response2, content2 = h.request(m_host, 'POST', urlencode(data), headers = postheader)

# download

downloadheader = {"Accept":"text/html, application/xhtml+xml, */*",

"Accept-Language":"zh-CN",

"User-Agent":"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",

"Accept-Encoding":"gzip, deflate",

"Host":"dl.iteye.com",

"Connection": "Keep-Alive",

"cache-control":"no-cache",

"Cookie": response2['set-cookie'],

}

response3, content3 = h.request(m_downurl, headers = downloadheader)

# save

filename = response3["content-disposition"].split("=")[1].replace('"', "")

file = open(filename,'wb')

file.write(content3)

file.close()

大小: 45 Bytes

分享到:

2012-09-09 22:57

浏览 1565

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值