導航:首頁 > 編程語言 > pythonrequests實例

pythonrequests實例

發布時間:2022-07-31 17:51:01

① [python] requests params

這是一個Python3讀取頁面的例子,僅供參考

importhttp.cookiejar
importurllib.request
ckjar=http.cookiejar.MozillaCookieJar()
opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(ckjar))
res=opener.open("http://www.test.com/search.php?ac=account&op=login","username=user&userpwd=pass".encode("utf-8"))
htm=res.read().decode("utf-8")
print(htm)

② 如何用python requests post一段字元串

用requests庫發送一次post請求,只要把字元串寫在表單裡面就可以了。

importrequests
data={key:str}#表單用字典格式,字元串作為value
r=requests.post(url,data=data)

③ python3的requests.get都有哪些請求參數

不用dict,而是用tuple list。實例如下: post_data = [('a', 1), ('a', 2)]requests.post(base_url, data=post_data)

④ 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模塊寫 下面的post請求

importrequests
url="http://www.xxx.com"
data={"a":"123","b":"456"}
res=requests.post(url,data)
print(res.text)

⑥ 如何創建 python+requests介面自動化測試框架

需要對於讀出來的數據進行相應的處理。 當然示例中只是簡單列了一下關於POST,GET等二種方式,實際還有很多其它方式,如put,delete等,請求中也還會包括headers,這些都可以自憶添加上去。

⑦ 如何用Python requests 爬取網頁所有文字

您可以用requests庫的get方法,以請求的網址為參數,獲取網頁所有html代碼,再訪問結果是text屬性即可。

⑧ 求用python requests庫登陸人人的方法示例!

12345678import requestss = requests.session()data = {'email':'用戶名','password':'密碼'}s.post(',data)r = s.get(")print r.text #列印登陸成功後的首頁
編輯器把url格式給改了
再出一個和諧版
把和諧倆字去掉就可以了
123456import requestss = requests.session()data = {'email':'用戶名','password':'密碼'}s.post('http和諧://www.ren和諧ren.com和諧/PLogin.do',data)r = s.get("和諧http和諧://www.ren和諧ren.com")print r.text

⑨ python requests post 參數重名

不用dict,而是用tuple list。實例如下:

post_data=[('a',1),('a',2)]
requests.post(base_url,data=post_data)

⑩ python http requests 怎麼實現模擬登錄,提交表單

以下實例是一個完整的代碼,實現了從博客獲取內容發布至網路,分別實踐抓取博客內容、模擬登錄、表單提交這幾步;
#注意,以下程序是一個完全程序,如果只需要實現模擬登錄,提交表單,刪除抓取部分即可,相關的代碼已經清楚標注,可以根據自己實際情況修改。
代碼如下:
# -*- coding: utf-8 -*-
import re
import urllib
import urllib2
import cookielib
#第一步,獲取博客標題和正文 ,「IP」可以改為實際地址;
url = "IP"
sock = urllib.urlopen(url)
html = sock.read()
sock.close()
content = re.findall('(?<=blogstory">).*(?=<p class="right artical)', html, re.S)
content = re.findall('<script.*>.*</script>(.*)', content[0], re.S)
title = re.findall('(?<=<title>)(.*)-.* - CSDN.*(?=</title>)', html, re.S)
#根據文章獲取內容新建表單值
blog = {'spBlogTitle': title[0].decode('utf-8').encode('gbk'), #文章標題
'spBlogText': content[0].decode('utf-8').encode('gbk'),#文章內容
'ct': "1",
'cm': "1"}
del content
del title

#第二步,模擬登錄網路;
cj = cookielib.CookieJar()
#登陸網路的用戶名和密碼
post_data = urllib.urlencode({'username': '[username]', 'password': '[password]', 'pwd': '1'})
#登錄地址路徑
path = 'https://passport..com/?login'
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent', 'Opera/9.23')]
urllib2.install_opener(opener)
req = urllib2.Request(path, post_data)
conn = urllib2.urlopen(req)

#獲取網路登陸認證令牌
bd = urllib2.urlopen(urllib2.Request('http://hi..com/[username]/creat/blog')).read()
bd = re.findall('(?<=bdstoken\" value=\").*(?=ct)', bd, re.S)
blog['bdstoken'] = bd[0][:32]
#設置分類名
blog['spBlogCatName'] = 'php'
#第四步,比較表單,提交表單;req2 = urllib2.Request('http://hi..com/[username]/commit', urllib.urlencode(blog))
#最後,查看錶單提交後返回內容,檢驗;
print urllib2.urlopen(req2).read()
#注意:將[username]/[password]替換為自己真實用戶名和密碼

閱讀全文

與pythonrequests實例相關的資料

熱點內容
php開發客戶端 瀏覽:998
theisle測試服怎麼搜伺服器 瀏覽:447
廣播PDF 瀏覽:216
單片機編程300例匯編百度 瀏覽:33
騰訊雲連接不上伺服器 瀏覽:221
不能用來表示演算法的是 瀏覽:859
6軸機器人演算法 瀏覽:890
手機主題照片在哪個文件夾 瀏覽:294
安卓手機後期用什麼軟體調色 瀏覽:628
cad修改快捷鍵的命令 瀏覽:242
好錢包app怎麼登錄不了 瀏覽:859
樹莓派都用python不用c 瀏覽:757
access文件夾樹的構造 瀏覽:662
安卓多指操作怎麼設置 瀏覽:658
linux樹形目錄 瀏覽:727
平方根的簡單演算法 瀏覽:898
千牛訂單頁面信息加密取消 瀏覽:558
單片機自製紅外遙控燈 瀏覽:719
伺服器最小配置怎麼弄 瀏覽:853
ibm伺服器硬體如何升級 瀏覽:923