python-requests包

#使用pycurl
import pycurl
import urlilb
#初始化,并保存cookie到本地的t文件中
def initCurl():  
    c = pycurl.Curl()  
    c.setopt(pycurl.COOKIEFILE, "t")
    c.setopt(pycurl.COOKIEJAR, "t")  
    c.setopt(pycurl.FOLLOWLOCATION, 1) 
    c.setopt(pycurl.MAXREDIRS, 5)  
    return c  
def initCurl_post(body):  
    c = initCurl()
    #设置要post的数据
    c.setopt(c.POSTFIELDS,  urllib.urlencode(body))
    return c  
#设置头部
def GetData(curl, url,cookie_l=[],header=""):  
    time.sleep(1)
    head = ['Accept:*/*',  
            header,
            'User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11']  
    buf = StringIO.StringIO()  
    curl.setopt(pycurl.WRITEFUNCTION, buf.write)  
    curl.setopt(pycurl.URL, url)  
    curl.setopt(pycurl.HTTPHEADER,  head)  
    curl.setopt(pycurl.COOKIE,''.join(cookie_l))
    curl.setopt(pycurl.VERBOSE,0)
    curl.perform()  
    the_page =buf.getvalue()  
    buf.close()  
    return the_page 
#使用方法就是
body = {
	"username":"usernmae",
	"password","password"
}
c = initCurl_post(body)
GetData(c, url, header="X-Requested-With":"XMLHttpRequest"")
#使用requests
import requests

#确定登录页面地址和键值对
loginUrl = "http://"
clinic_url = "http://"
loginData={
    'mobilephone': "phone",
    'password': "pass" 
}
head = {
	"Accept":"*/*",  
    "User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11",
    "X-Requested-With":"XMLHttpRequest"
}
s = requests.session()
print s.post(url=loginUrl,data=loginData,headers=head).text
print s.get(url=clinic_url, headers=head).text

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值