爬蟲必知的request模塊

# 導入 Request模塊
# 若本機無自帶Request模塊,可自行下載或者使用pip進行安裝
# python版本Python3
import requests
import json

#######################Get請求#######################

# 發送無參數的get請求
baiDu_response = requests.get('http://www.baidu.com')

# 發送無參數的get請求 設置超時時間 timeout 單位秒
baiDu_response = requests.get('http://www.baidu.com', timeout=1)

# 查看發送請求的url地址
print('無參數的get請求地址爲: ' + baiDu_response.url)

# 查看當前返回狀態碼
# 若狀態碼爲200 等同於 baiDu_response.status_code == requests.codes.ok 返回爲True
print('返回的狀態碼爲: '+str(baiDu_response.status_code))

# 查看當前返回內容編碼
print('返回內容編碼格式爲:' + baiDu_response.encoding)

# 查看當前返回內容    text返回的是字符串
print('Text返回的數據內容爲:' + baiDu_response.text)

# 查看當前返回內容    content返回的是字節流
# print('Content返回的數據內容爲:' + baiDu_response.content)

# 若返回內容爲json格式 可用以下語句
# print('返回的Json數據爲:' + baiDu_response.json())

# 獲取服務器返回的原始數據 增長stream=True
# data = requests.get('https://api.github.com/events', stream=True)
# print(data.raw.read())

# 發送帶參數(字典形式)的get請求
sendDictParams = {'key1': 'value1', 'key2': 'value2'}
baiDu_dictParams_response = requests.get('http://www.baidu.com', params=sendDictParams)

# 查看發送請求的url地址
print('普通參數的get請求地址爲: ' + baiDu_dictParams_response.url)

# 發送list格式參數的get請求
sendListParams = {'key1': 'value1', 'key2': ['value2', 'value3']}
baiDu_listParams_response = requests.get('http://www.baidu.com', params=sendListParams)

# 查看發送請求的url地址
print('帶list參數的get請求地址爲: ' + baiDu_listParams_response.url)

#######################Post請求#######################

# tips:強烈建議使用二進制模式打開文件,由於若是以文本文件格式打開時,可能會由於「Content-Length」這個header而出錯
# post 請求參數是經過data方式來傳遞的

# 第一種方式:字典格式
postResponse = requests.post("http://pythontab.com/postTest", data={'key': 'value'})
print('普通參數請求返回狀態碼爲:' + str(postResponse.status_code))

# 第二種方式 json格式 注意json方法爲dumps() 不是dump()
jsonParams = {'key': 'value'}
postJsonResponse = requests.post("http://pythontab.com/postTest", data=json.dumps(jsonParams))
print('json參數請求返回狀態碼爲:' + str(postJsonResponse.status_code))

# 第三種方式 發送文件  (該文件同級目錄下須要有test.csv文件 )rb 只讀模式  wb 若沒有 自動建立
files = {'file': open('test.csv', 'rb')}
fileResponse = requests.post('http://pythontab.com/postTest', files=files)
print('文件參數請求返回狀態碼爲:' + str(fileResponse.status_code))


#######################Headers#######################
headers_response = requests.get('http://www.baidu.com')

# 查看請求響應頭 :字典格式 輸入時轉換爲字符打印到控制檯
print('請求響應頭爲: ' + str(headers_response.headers))

# 獲取請求響應頭其中某一個參數
print('請求響應頭的Server參數   寫法一:' + headers_response.headers['Server'])

# 等同於
print('請求響應頭的Server參數   寫法二:' + headers_response.headers.get('Server'))

# 自定義headers 併發送
headers = {'user-agent': 'myAgent'}
custom_headers_response = requests.get('http://www.baidu.com', headers=headers)
print('自定義header發送請求狀態碼爲:' + str(custom_headers_response.status_code))


#######################Cookies#######################
cookies_response = requests.get('http://www.baidu.com')

# 查看請求響應頭 :字典格式 輸入時轉換爲字符
print('請求地址的Cookies爲: ' + str(cookies_response.cookies))

# 自定義Cookies 併發送
cookies = {'user-cookies': 'myCookies'}
custom_cookies_response = requests.get('http://www.baidu.com', cookies=cookies)
print('自定義Cookies發送請求狀態碼爲:' + str(custom_cookies_response.status_code))

#######################代理#######################

proxies = {
  "http": "http://10.10.1.10:3128",
  "https": "http://10.10.1.10:1080",
}
# 經過代理方式發起請求 此處運行不經過,僅舉例使用
# requests.get("http://baidu.com", proxies=proxies)


#######################Session#######################
# 經過requests獲取session
session = requests.Session()
# 舉例:登陸名 密碼  key爲登錄表單中對應的input的name值
login_data = {'email': 'email@example.com', 'password': 'password'}
# 發送數據
session.post("http://pythontab.com/testLogin", login_data)
# 獲取發送的session
session_response = session.get('http://pythontab.com/notification/')
print('session請求返回的狀態碼爲:' + str(session_response.status_code))

#######################下載頁面#######################

baiDuHtmlContent = requests.get("http://www.baidu.com")
with open("百度.html", "wb") as html:
    html.write(baiDuHtmlContent.content)
html.close()

運行結果html

這裏寫圖片描述

參考:www.pythontab.com/html/2017/pythonjichu_0510/1138.htmlpython

相關文章
相關標籤/搜索