pythonurlrequest
『壹』 python:Request的函數是什麼作用
你說的是
1
「class Request( url[, data][, headers] [, origin_req_host][, unverifiable]) 」吧。
這汪中是一個類阿。是提取url中的信息的阿
「This class is an abstraction of a URL request.」
就像你在網路裡面搜索「python」一樣。
用戶點完enter鍵觸發。
這時候
URL = "http://www..com/s?wd=python"
Request(URL)
這樣就生成了一個類冊晌。你就可以用他來州陵鋒解析用戶需求。
2
request( method, url[, body[, headers]])
This will send a request to the server using the HTTP request method method and the selector url. If the body argument is present, it should be a string of data to send after the headers are finished. The header Content-Length is automatically set to the correct value. The headers argument should be a mapping of extra HTTP headers to send with the request.
『貳』 關於python裡面的requests和urllib的request
前者是一個庫,後者應該只是一個方法?前者比後者強大。
『叄』 python通過get,post方式發送http請求和接收http響應
本文實例講述了python通過get,post方式發送http請求和接收http響應的方法。姿敏分享給輪敏大家供大家參考。
具體如下:
測試用CGI,名字為test.py,放在apache的cgi-bin目錄下:
#!/usr/bin/python
import cgi
def main():
print Content-type: text/htmln
form = cgi.FieldStorage()
if form.has_key(ServiceCode) and form[ServiceCode].value != :
print h1 Hello,form[ServiceCode].value,/h1
else:
print h1 Error! Please enter first name./h1
main()
python發送post和get請求
get請求:
使用get方式時,請求數據直接放在url中。
方法一、
?
7
8
import urllib
import urllib2
url =
req = urllib2.Request(url)
print req
res_data = urllib2.urlopen(req)
res = res_data.read()
print res
方法二、
?
7
import httplib
url =
conn = httplib.HTTPConnection(192.168.81.16)
conn.request(method=GET,url=url)
response = conn.getresponse()
res= response.read()
print res
post請求:臘冊枝
使用post方式時,數據放在data或者body中,不能放在url中,放在url中將被忽略。
方法一、
import urllib
import urllib2
test_data = {ServiceCode:aaaa,b:bbbbb}
test_data_urlencode = urllib.urlencode(test_data)
requrl =
req = urllib2.Request(url = requrl,data =test_data_urlencode)
print req
res_data = urllib2.urlopen(req)
res = res_data.read()
print res
方法二、
11
import urllib
import httplib
test_data = {ServiceCode:aaaa,b:bbbbb}
test_data_urlencode = urllib.urlencode(test_data)
requrl =
headerdata = {Host:192.168.81.16}
conn = httplib.HTTPConnection(192.168.81.16)
conn.request(method=POST,url=requrl,body=test_data_urlencode,headers = headerdata)
response = conn.getresponse()
res= response.read()
print res
對python中json的使用不清楚,所以臨時使用了urllib.urlencode(test_data)方法;
模塊urllib,urllib2,httplib的區別
httplib實現了http和https的客戶端協議,但是在python中,模塊urllib和urllib2對httplib進行了更上層的封裝。
介紹下例子中用到的函數:
1、HTTPConnection函數
httplib.HTTPConnection(host[,port[,stict[,timeout]]])
這個是構造函數,表示一次與伺服器之間的交互,即請求/響應
host 標識伺服器主機(伺服器IP或域名)
port 默認值是80
strict 模式是False,表示無法解析伺服器返回的狀態行時,是否拋出BadStatusLine異常
例如:
conn = httplib.HTTPConnection(192.168.81.16,80) 與伺服器建立鏈接。
2、HTTPConnection.request(method,url[,body[,header]])函數
這個是向伺服器發送請求
method 請求的方式,一般是post或者get,
例如:
method=POST或method=Get
url 請求的資源,請求的資源(頁面或者CGI,我們這里是CGI)
例如:
url=
或者
url=
body 需要提交到伺服器的數據,可以用json,也可以用上面的格式,json需要調用json模塊
headers 請求的http頭headerdata = {Host:192.168.81.16}
例如:
?
test_data = {ServiceCode:aaaa,b:bbbbb}
test_data_urlencode = urllib.urlencode(test_data)
requrl =
headerdata = {Host:192.168.81.16}
conn = httplib.HTTPConnection(192.168.81.16,80)
conn.request(method=POST,url=requrl,body=test_data_urlencode,headers = headerdata)
conn在使用完畢後,應該關閉,conn.close()
3、HTTPConnection.getresponse()函數
這個是獲取http響應,返回的對象是HTTPResponse的實例。
4、HTTPResponse介紹:
HTTPResponse的屬性如下:
read([amt]) 獲取響應消息體,amt表示從響應流中讀取指定位元組的數據,沒有指定時,將全部數據讀出;
getheader(name[,default]) 獲得響應的header,name是表示頭域名,在沒有頭域名的時候,default用來指定返回值
getheaders() 以列表的形式獲得header
例如:
?
1
2
3
4
5
date=response.getheader(date);
print date
resheader=
resheader=response.getheaders();
print resheader
列形式的響應頭部信息:
?
1
2
3
[(content-length, 295), (accept-ranges, bytes), (server, Apache), (last-modified, Sat, 31 Mar 2012 10:07:02 GMT), (connection, close), (etag, e8744-127-4bc871e4fdd80), (date, Mon, 03 Sep 2012 10:01:47 GMT), (content-type, text/html)]
date=response.getheader(date);
print date
取出響應頭部的date的值。
希望本文所述對大家的Python程序設計有所幫助。
『肆』 python3爬蟲urllib.request.urlopen("網址").read() 本來是utf-8,為什麼還要加上urlencode(「utf-8」)
你這行代碼是不需要urlencode()的。
對於返回的request對象,其read()方法獲得的其實是一個位元組流對象,而非字元串對象,所以這時需要調用該位元組流對象的decode()方法,按指定編碼方式進行解碼。
至於urlencode(),這是urllib中的一個函數,它的作用是將字元串進行url編碼。這個編碼其實就是個轉義的過程,將那些因可能造成解釋器誤會或安全問題而不適合出現在請求中的符號進行轉義,並且把超出url編碼表的字元降維。
『伍』 【Python爬蟲】分析網頁真實請求
1、抓取網頁、分析請求
2、解析網頁、尋找數據
3、儲存數據、多頁處理
翻頁有規律:
很多網址在第一頁時並沒有變化,多翻下一頁後規律就出來,比如 豆瓣第一頁 和 豆瓣第三頁
發現start為40,limit=20,所以猜測start=0就是第一頁,每頁顯示20條數據,對於第三頁顯示的參數可以一個個刪除驗證,可以減去不必要的參數, 但是刪除前一定要做好數據的對比
(1) 文本框輸入後產生一個請求,如常見的登錄、注冊頁面
Referer:表示當前請求的來源
Request URL:表示實際請求地址
翻頁後URL不變,該如何尋找請求?
如: http://www.zkh360.com/zkh_catalog/3.html
通過對比可以發現網站是通過pageIndex參數控制翻頁的,?表示連接
接下來用抓包工具分析下 ,從第四頁開始看URL就知道了,但是前面幾面需要查看請求的參數,這里偏多,就切換到【Inspectors--Webforms】選項,看的比較直觀
類似的網站還有 今日頭條 ,有興趣的朋友可以去研究下
(可通過獲取max_behot_time的值而改變as和cp)
『陸』 python requests get方式怎麼設置請求頭
Header可以通過Request提供的.add_header()方法進行添加,示例代碼如下:
123456789101112#-*-coding:utf-8-*-
importurllib2importurlliburl='http://ah.example.com'half_url=u'/servlet/av/jd?
ai=782&ji=2624743&sn=I'#構造get請求req=urllib2.
Request(url+half_url.
encode('utf-8'))#添加headerreq.add_header('AcceptEncoding','gzip,deflate')req.
add_header('User-Agent','Mozilla/5.0')response=urllib2.
urlopen(req)
printresponse.
『柒』 Python request 響應狀態
我們可以檢測響應狀態碼蘆亂握:
r=requests.get(url)r.status_code
返回200,正常響應
為方便引用,Requests還附帶了一個陪慶內置的狀態碼查詢對象:
r = requests.get(url)
print r.status_code == requests.codes.ok
如果發送了一個失敗請陪鉛求(非200響應),我們可以通過Response.raise_for_status()來拋出異常:
bad_r = requests.get(url)
bad_r.raise_for_status()