導航:首頁 > 編程語言 > 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實例相關的資料

熱點內容
什麼是適合自己的app 瀏覽:451
雲伺服器怎麼補漏洞 瀏覽:176
如何看自己的安卓是什麼等級 瀏覽:633
被丁磊挖走的程序員 瀏覽:19
gsk980ta編程 瀏覽:507
單片機離線 瀏覽:84
解壓助眠敲擊外國長指甲 瀏覽:421
3次元編程 瀏覽:803
夫妻傳送英文命令 瀏覽:316
程序員題代做 瀏覽:950
戴爾伺服器怎麼擴大內存 瀏覽:641
解壓包密碼消失 瀏覽:135
昆侖通態觸摸屏用戶窗加密 瀏覽:564
農信app怎麼看日額 瀏覽:865
iphone備忘錄包含視頻無法加密 瀏覽:283
抖音快手紅包源碼 瀏覽:137
程序員資源分析 瀏覽:586
linux交換分區大小 瀏覽:922
mt怎麼編譯 瀏覽:337
qq郵箱打開pdf 瀏覽:461