基于Python模擬瀏覽器發(fā)送http請(qǐng)求
1.使用 urllib2 實(shí)現(xiàn)
#! /usr/bin/env python
# -*- coding=utf-8 -*-
import urllib2
url="https://www.baidu.com"
req_header = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
"Accept":"text/html;q=0.9,*/*;q=0.8",
"Accept-Charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.3",
"Accept-Encoding":"gzip",
"Connection":"close",
"Referer":None #注意如果依然不能抓取的話,這里可以設(shè)置抓取網(wǎng)站的host
}
req_timeout = 5
req = urllib2.Request(url,None,req_header)
resp = urllib2.urlopen(req,None,req_timeout)
html = resp.read()
print(html)
2.使用 requests 模塊
(1).get請(qǐng)求
#-*- coding:utf-8 -*-
import requests
url = "https://www.baidu.com"
payload = {"key1": "value1", "key2": "value2"}
r = requests.get(url, params=payload)
print r.text
(2).post請(qǐng)求
#-*- coding:utf-8 -*-
import requests
url1 = "http://www.exanple.com/login"#登陸地址
url2 = "http://www.example.com/main"#需要登陸才能訪問(wèn)的地址
data={"user":"user","password":"pass"}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
"Accept-Encoding":"gzip",
"Accept-Language":"zh-CN,zh;q=0.8",
"Referer":"http://www.example.com/",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
}
res1 = requests.post(url1, data=data, headers=headers)
res2 = requests.get(url2, cookies=res1.cookies, headers=headers)
print res2.content#獲得二進(jìn)制響應(yīng)內(nèi)容
print res2.raw#獲得原始響應(yīng)內(nèi)容,需要stream=True
print res2.raw.read(50)
print type(res2.text)#返回解碼成unicode的內(nèi)容
print res2.url
print res2.history#追蹤重定向
print res2.cookies
print res2.cookies["example_cookie_name"]
print res2.headers
print res2.headers["Content-Type"]
print res2.headers.get("content-type")
print res2.json#講返回內(nèi)容編碼為json
print res2.encoding#返回內(nèi)容編碼
print res2.status_code#返回http狀態(tài)碼
print res2.raise_for_status()#返回錯(cuò)誤狀態(tài)碼
(3).使用session對(duì)象的寫法
#-*- coding:utf-8 -*-
import requests
s = requests.Session()
url1 = "http://www.exanple.com/login"#登陸地址
url2 = "http://www.example.com/main"#需要登陸才能訪問(wèn)的地址
data={"user":"user","password":"pass"}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
"Accept-Encoding":"gzip",
"Accept-Language":"zh-CN,zh;q=0.8",
"Referer":"http://www.example.com/",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
}
prepped1 = requests.Request("POST", url1,
data=data,
headers=headers
).prepare()
s.send(prepped1)
"""
也可以這樣寫
res = requests.Request("POST", url1,
data=data,
headers=headers
)
prepared = s.prepare_request(res)
# do something with prepped.body
# do something with prepped.headers
s.send(prepared)
"""
prepare2 = requests.Request("POST", url2,
headers=headers
).prepare()
res2 = s.send(prepare2)
print res2.content
"""另一種寫法"""
#-*- coding:utf-8 -*-
import requests
s = requests.Session()
url1 = "http://www.exanple.com/login"#登陸地址
url2 = "http://www.example.com/main"#需要登陸才能訪問(wèn)的頁(yè)面地址
data={"user":"user","password":"pass"}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
"Accept-Encoding":"gzip",
"Accept-Language":"zh-CN,zh;q=0.8",
"Referer":"http://www.example.com/",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
}
res1 = s.post(url1, data=data)
res2 = s.post(url2)
print(resp2.content)
3.其他的一些請(qǐng)求方式
>>> r = requests.put("http://httpbin.org/put")
>>> r = requests.delete("http://httpbin.org/delete")
>>> r = requests.head("http://httpbin.org/get")
>>> r = requests.options(http://httpbin.org/get)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)Mysql全量數(shù)據(jù)同步的腳本分享
這篇文章主要為大家詳細(xì)介紹了基于Python如何實(shí)現(xiàn)Mysql全量數(shù)據(jù)同步的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-06-06
Python程序員面試題 你必須提前準(zhǔn)備!(答案及解析)
這篇文章主要為大家解析了你必須提前準(zhǔn)備的Python程序員面試題答案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
一文詳解如何在Python中進(jìn)行數(shù)學(xué)建模
數(shù)學(xué)建模是數(shù)據(jù)科學(xué)中使用的強(qiáng)大工具,通過(guò)數(shù)學(xué)方程和算法來(lái)表示真實(shí)世界的系統(tǒng)和現(xiàn)象,本文將指導(dǎo)大家完成Python中的數(shù)學(xué)建模過(guò)程,感興趣的可以了解下2024-11-11
Python中l(wèi)ist循環(huán)遍歷刪除數(shù)據(jù)的正確方法
這篇文章主要給大家介紹了關(guān)于Python中l(wèi)ist循環(huán)遍歷刪除數(shù)據(jù)的正確方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
詳解Python openpyxl庫(kù)的基本應(yīng)用
這篇文章主要介紹了Python openpyxl庫(kù)的基本應(yīng)用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-02-02
使用Python和XML實(shí)現(xiàn)文件復(fù)制工具的完整代碼
在本篇博客中,我們將學(xué)習(xí)如何使用 wxPython 構(gòu)建一個(gè)簡(jiǎn)單的文件復(fù)制工具,并將文件路徑和目標(biāo)目錄的配置信息保存到 XML 文件中,通過(guò)這種方式,我們可以在下次運(yùn)行程序時(shí)輕松加載之前保存的配置,需要的朋友可以參考下2024-08-08
Python JWT認(rèn)證與pyjwt包詳細(xì)介紹
JWT的聲明一般被用來(lái)在身份提供者和服務(wù)提供者間傳遞被認(rèn)證的用戶身份信息,以便于從資源服務(wù)器獲取資源,也增加一些額外的其它業(yè)務(wù)邏輯所必須的聲明信息,該token也可直接被用于認(rèn)證,也可被加密,這篇文章主要介紹了Python JWT認(rèn)證與pyjwt包簡(jiǎn)介,需要的朋友可以參考下2023-05-05

