requests _demo

# -*- coding:utf-8 -*-

# requests 網絡請求包,基於urllib封裝第三方請求包
# pip install requesrs 下載包

import requests
# 支持全部類型的請求
# requests.get()
# requests.post()
# requests.delete()
# requests.put()
# requests.head()
# requests.options()
'''
# 發送請求時能夠傳遞的參數
def request(self, method, url,
            params=None, data=None, headers=None, cookies=None, files=None,
            auth=None, timeout=None, allow_redirects=True, proxies=None,
            hooks=None, stream=None, verify=None, cert=None, json=None)
'''
proxies = {
    'http':'120.92.119.187:10000'
}
response =  requests.get('http://kaoshi.zhiyou900.com:8888/edustu/login/login.spr',proxies = proxies)
# 返回數據的bytes類型數據
# print(response.content)
# 返回數據的str類型數據
# print(response.text)
# 狀態碼
print(response.status_code)
# headers字典
print(response.headers)
print(response.cookies)
print(response.request)
print(response.url)

print('================================')
# 訪問的歷史記錄,若是服務器沒有返回,是一個空列表
print(response.history)
# 編碼格式
print(response.encoding)
# 緣由短語
print(response.reason)
# 這次請求消耗的時間
print(response.elapsed)

url = 'http://kaoshi.zhiyou900.com:8888/edustu/login/login.spr'

# requests中使用代理
# proxies = {
#     'http':'122.114.31.177:808'
# }
# 須要上傳參數
data = {
    'j_username':'15896901897',
    'j_password':'123456'
}

# 發送post請求
response = requests.post(url,data=data,headers={},proxies=proxies)

with open('1.html','w') as f:
    f.write(response.text)


# 獲取json數據
response =requests.get('http://api.map.baidu.com/telematics/v3/weather?location=鄭州市&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?')
import json
# 轉換爲python
# json.loads(response.text)
# json() 針對返回的json數據,能夠直接使用json()將其轉換python對象
rs = response.json()
print(type(rs))
相關文章
相關標籤/搜索