当前位置: 代码迷 >> Web前端 >> python 模拟Web登录与上载
  详细解决方案

python 模拟Web登录与上载

热度:1128   发布时间:2012-12-25 16:18:28.0
python 模拟Web登录与下载
1
前提
需要第三方包
httplib2

2
本脚本实现本站自动登录下载文件
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()