我会为您详细讲解如何利用Python实现自动填写问卷星问卷并提交问卷的两种方法。
方法一:使用selenium库模拟人工操作
- 确认所要填写的问卷星问卷的URL,保存到一个变量中:
url = "https://www.wjx.cn/..."
- 使用selenium库打开浏览器(需要预先安装好对应的浏览器驱动),访问问卷星问卷网址:
from selenium import webdriver
driver = webdriver.Chrome() # 这里以Chrome浏览器为例,需先下载对应驱动
driver.get(url)
- 输入问卷的全部选项或问答内容,可通过查看DOM元素获取需要的元素标签或属性(以单选题为例):
from selenium.webdriver.common.keys import Keys
# 获取单选框按钮元素
radio_buttons = driver.find_elements_by_css_selector("ul[role='radiogroup'] li label input")
# 遍历单选框按钮,模拟操作选择选项
for radio in radio_buttons:
if radio.get_attribute('value') == '1': # 假设选项1为正确答案
radio.click()
break
- 提交问卷
submit_button = driver.find_element_by_css_selector("button.submitbutton")
submit_button.click()
# 关闭浏览器
driver.quit()
方法二:通过模拟网络请求实现自动填写问卷星问卷
- 确认所要填写的问卷星问卷的URL和问卷的ID,保存到变量中(ID为问卷星生成的识别id):
url = "https://www.wjx.cn/..."
wjx_id = "52633322"
- 分析接口,获取问卷题目信息和默认答案
import requests
import json
response = requests.get(f"https://www.wjx.cn/joinnew/processjq.ashx?curid={wjx_id}&starttime={int(time.time()*1000)}")
data = json.loads(response.text)
questions = [] # 保存问卷题目信息和默认答案
for table in data['Tables']:
for row in table['Rows'][1:]:
question = {}
answer = ""
for cell in row['Cells']:
if cell['Column']['ColumnName'] == "QuestionName":
question['title'] = cell['Answer']
elif cell['Column']['ColumnName'] == "Type":
question['type'] = cell['Answer']
elif cell['Column']['ColumnName'] == "Require":
question['required'] = cell['Answer']
try:
# 多选题、填空题等可能有默认答案,需判断
answers = row['Cells'][-1]['Data'].split(',')
if answers[0] != '':
answer = answers
except:
pass
question['answer'] = answer
questions.append(question)
print(questions)
- 模拟post请求提交问卷
data = {
'submitdata': '',
't': int(time.time()*1000),
'pyid': '',
'wjxid': wjx_id,
'curID': wjx_id,
'newSid': '0',
'temp': '0',
'sms': '',
'isCheckCode': '',
'checkCode': '',
'tj': '{" latitude":""," longitude":""}',
'scroll': '0',
'submittype': '1',
'ktm': ''
}
# 根据分析的接口,包装要提交的答案
for question in questions:
if question['type'] == "1": # 单选题类型
data['submitdata'] += f"{question['title']}${question['answer'][0]},"
elif question['type'] == "2": # 多选题类型
data['submitdata'] += f"{question['title']}$:"
for answer in question['answer']:
data['submitdata'] += f"{answer},"
data['submitdata'] = data['submitdata'][:-1] + ';'
elif question['type'] == "3": # 填空题类型
data['submitdata'] += f"{question['title']}${question['answer'][0]},"
elif question['type'] == "4": # 问答题类型
data['submitdata'] += f"{question['title']}${question['answer'][0]},"
post_response = requests.post("https://www.wjx.cn/joinnew/processjq.ashx", data=data)
print("提交成功") if post_response.status_code == 200 else print("提交失败")
以上就是两种自动填写问卷星问卷的方法,可以根据实际需求选择使用。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python自动填写问卷星问卷以及提交问卷等功能 - Python技术站