#使用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
python-requests包
最新推荐文章于 2022-07-22 21:36:06 发布