天天看點

requests庫的get請求(加上head,加上get參數請求)

#coding:utf-8
# 導入requests
import requests

# 建構url
url = 'http://www.baidu.com'

# 發送請求,擷取響應
# response = requests.get(url)
response = requests.head(url)

# 檢查狀态碼
# print (response.status_code)

# 檢查url
# print (response.url)

# 檢查請求頭
# print (response.request.headers)

# 檢查響應頭
# print (response.headers)

# 檢查源碼
# print (response.content)
# print (response.content.decode())
#
# response.encoding='utf-8'
# print (response.text)
# print (response.encoding)
           

  帶headers的請求

#coding:utf-8
import requests
import time

# 建構url
url = 'http://www.baidu.com'

# 建構請求頭
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36'
}


# 發送請求
response = requests.get(url, headers=headers)
print (len(response.content))

time.sleep(2)
response1 = requests.get(url)
print (len(response1.content))
           

  

帶get傳參的請求

#coding:utf-8
import requests

# 建構url
url = 'https://www.baidu.com/s'
# 建構headers
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36'
}

# 建構參數
params = {
    "wd": "深圳"
}

# 發送請求
response = requests.get(url, headers=headers, params=params)

# 驗證url
# print(response.url)

with open('baidu.html','w')as f:
    f.write(response.content.decode())
           

get帶有代理的請求:

#coding:utf-8
import requests

# 建構一個url
url = 'http://www.itcast.cn'
# 建構headers
headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36'
        }
# 建構代理
# proxies = {
#     "http": "http://106.14.51.145:8118",
#     "https": "https://106.14.51.145:8118",
# }
# 付費代理
proxies = {
    "http": "http://morganna_mode_g:[email protected]:16816",
    "https": "https://morganna_mode_g:[email protected]:16816",
}

# 發送請求
response = requests.get(url,headers=headers,proxies=proxies)


#? 如何驗證代理是否使用成功
# 運用逾時抛出異常來判斷是否成功,一般0.5-1秒
# response = requests.get(url, timeout=3)
           

  

轉載于:https://www.cnblogs.com/andy9468/p/8306749.html