通过python3实现投票功能代码实例

yipeiwu_com6年前Python基础

这篇文章主要介绍了通过python3实现投票功能代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

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")
  # 后台校验的动态验证码,随自己业务调整
  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提交的数据,第一个为选中的复选框选项(多个中间逗号分隔),第二个为动态码(后台校验)
  data_json = {'option': option, 'secret_key': secret_key}
  # string转为byte类型,因为客户端校验要求
  data = urllib.parse.urlencode(data_json).encode("utf-8")
  # post请求,提交投票数据
  req = urllib.request.Request(post_url, headers=headers, data=data)
  response = urllib.request.urlopen(req)
  # 查看返回结果,转码为中文
  print(bytes.decode(response.read()))
def deal():
  # get连接、post链接、选项 根据自己业务修改
  get_url = ""
  post_url = ""
  option = ""
  vote(get_url, post_url, option)
deal()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python中新式类与经典类的区别详析

1.新式类与经典类 在Python 2及以前的版本中,由任意内置类型派生出的类(只要一个内置类型位于类树的某个位置),都属于“新式类”,都会获得所有“新式类”的特性;反之,即不由任意内置...

python中requests小技巧

python中requests小技巧

关于  Python requests ,在使用中,总结了一些小技巧把,记录下。 1:保持请求之间的Cookies,我们可以这样做。 2:请求时,会加上headers,一般我...

Python Pandas 转换unix时间戳方式

Python Pandas 转换unix时间戳方式

使用pandas自带的pd.to_datetime把 unix 时间戳转为时间时默认是转换为 GMT标准时间   北京时间比这个时间还要加 8个小时, 使用pyth...

详解python中executemany和序列的使用方法

详解python中executemany和序列的使用方法 一 代码 import sqlite3 persons=[ ("Jim","Green"), ("Hu","...

python微信跳一跳系列之自动计算跳一跳距离

python微信跳一跳系列之自动计算跳一跳距离

到现在为止,我们通过前面几篇博文的描述和分析,已经可以自动实现棋子、棋盘位置的准确判断,计算一下两个中心点之间的距离,并绘制在图形上,效果如下。 效果 图中的棋子定位采用HSV颜色识别...