本篇文章為大家展示了使用python3怎么實(shí)現(xiàn)一個(gè)投票功能,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
import urllib.request # cd C:\Python36-32\Scripts # pip install BeautifulSoup from bs4 import BeautifulSoup def vote(get_url, post_url, option): # 訪問(wèn)投票頁(yè)面,拿到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àn)的動(dòng)態(tài)驗(yàn)證碼,隨自己業(yè)務(wù)調(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ù),第一個(gè)為選中的復(fù)選框選項(xiàng)(多個(gè)中間逗號(hào)分隔),第二個(gè)為動(dòng)態(tài)碼(后臺(tái)校驗(yàn)) data_json = {'option': option, 'secret_key': secret_key} # string轉(zhuǎn)為byte類型,因?yàn)榭蛻舳诵r?yàn)要求 data = urllib.parse.urlencode(data_json).encode("utf-8") # post請(qǐng)求,提交投票數(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鏈接、選項(xiàng) 根據(jù)自己業(yè)務(wù)修改 get_url = "" post_url = "" option = "" vote(get_url, post_url, option) deal()
上述內(nèi)容就是使用python3怎么實(shí)現(xiàn)一個(gè)投票功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享標(biāo)題:使用python3怎么實(shí)現(xiàn)一個(gè)投票功能-創(chuàng)新互聯(lián)
鏈接URL:http://www.chinadenli.net/article36/dgchsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁(yè)設(shè)計(jì)公司、營(yíng)銷型網(wǎng)站建設(shè)、做網(wǎng)站、軟件開(kāi)發(fā)、定制開(kāi)發(fā)、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容