個人筆記,無其他意圖
requests模塊的學習
發送get請求,post請求獲取響應
response = requests.get(url) #發送get請求,獲取url地址的響應
response = requests.post(url, data = {請求體的字典}) #發送post請求
獲取響應內容的方法(承接上面,響應用response表示)
response.text
該方式往往會出現亂碼,出現亂碼使用response.encoding = "utf-8"
response.content.decode()
默認解碼方式爲UTF-8,把響應的二進制字節轉換爲字符串類型
response.request.url #發送請求的url地址
response.url #response響應的url地址
response.request.headers #請求頭
response.headers #響應頭
獲取網頁源碼的正確打開方式(依次試用即可)
response.content.decode()
response.content.decode("gbk")
response.text
發送帶Header的請求
- 爲了模擬瀏覽器,獲取和瀏覽器一模一樣的內容
# 一般headers添加一個User-Agent可能就夠了,如果不夠再繼續添加,一般最後添加Cookie
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER",
"Referer":"https://www.duba.com/?f=liebao"
...
"Cookie":"...自行添加..."
}
response = requests.get(url, headers=headers)
使用超時參數
requests.get(url, headers=headers, timeout=3) #3秒內必須返回響應,否則會報錯
retrying模塊的學習
- 可封裝起來,以後直接調用
import requests
from retrying import retry
'''
It is a way that requesting adress of url
'''
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER"
}
@retry(stop_max_attempt_number=3) #讓被裝飾的函數反覆執行三次,三次全部報錯纔會報錯,中間有一次正確就通過
def _parse_url(url):
response = requests.get(url, headers=headers, timeout=5)
return response.content.decode()
def parse_url(url):
try:
html_str = _parse_url(url)
except:
html_str = none #url地址打不開返回none
return html_str
if __name__ == '__main__':
url = '...輸入網址...'
print(parse_url(url))
處理Cookie相關請求
- 直接攜帶Cookie請求URL地址
- Cookie放在headers中
headers = { "User-Agent":"...", "Cookie":"... Cookie 字符串 ..." }
- Cookie字典傳給Cookies參數
requests.get(url, cookies=cookie_dict)
- 先發送post請求,獲取Cookie,帶上Cookie請求登錄後的頁面
session = requests.session() #實例化session,使session具有的方法和requests一樣 session.post(url,data,headers) #服務器設置在本地的Cookie會被保存在session session.get(url) #會帶上之前保存在session中的Cookie,能夠請求成功。