本篇文章為大家展示了使用python3怎么實現(xiàn)一個投票功能,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
import urllib.request # cd C:\Python36-32\Scripts # pip install BeautifulSoup from bs4 import BeautifulSoup def vote(get_url, post_url, option): # 訪問投票頁面,拿到cookie resp = urllib.request.urlopen(get_url) cookie = resp.getheader('Set-Cookie') # print(cookie) # 讀取response信息 html = resp.read() # HTML解析器,拿到vote_option bs = BeautifulSoup(html, "html.parser") # 后臺校驗的動態(tài)驗證碼,隨自己業(yè)務調(diào)整 secret_key = bs.find('input', id='secret_key').get("value") # print(vote_option) # hearders部分,cookie等 headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Firefox/52.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Cookie': cookie} # post提交的數(shù)據(jù),第一個為選中的復選框選項(多個中間逗號分隔),第二個為動態(tài)碼(后臺校驗) data_json = {'option': option, 'secret_key': secret_key} # string轉(zhuǎn)為byte類型,因為客戶端校驗要求 data = urllib.parse.urlencode(data_json).encode("utf-8") # post請求,提交投票數(shù)據(jù) req = urllib.request.Request(post_url, headers=headers, data=data) response = urllib.request.urlopen(req) # 查看返回結(jié)果,轉(zhuǎn)碼為中文 print(bytes.decode(response.read())) def deal(): # get連接、post鏈接、選項 根據(jù)自己業(yè)務修改 get_url = "" post_url = "" option = "" vote(get_url, post_url, option) deal()
上述內(nèi)容就是使用python3怎么實現(xiàn)一個投票功能,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享標題:使用python3怎么實現(xiàn)一個投票功能-創(chuàng)新互聯(lián)
鏈接URL:http://www.rwnh.cn/article36/dgchsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司、營銷型網(wǎng)站建設(shè)、做網(wǎng)站、軟件開發(fā)、定制開發(fā)、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容