word类型考题带选项答案批量存入mysql数据库中

news/2024/7/19 9:50:51 标签: 数据库, 爬虫, php

由于工作需要批量将word文档考题导入mysql中

题目如下(简单列举下PHP试题):

1.mysql_connect( )与@mysql_connect( )的区别是 

  A  @mysql_connect( )不会忽略错误,将错误显示到客户端   

  B  mysql_connect( )不会忽略错误,将错误显示到客户端   

  C  没有区别   

  D  功能不同的两个函数  

正确答案是  B

答案解析:@阻止警告输出,有些函数,在遇到入参不正确时,会提示警告,但程序也可以正常运行。其实只要把警告去掉就可以,所以就有@这个符号

...

 

以上word 文档中很多类似题有单选,有多选,以下方式将所有单选入库

通过分析PHP处理比较麻烦,并没有好的方式批量将考题入库,由于之前有爬虫经验,想到可以通过解析html入库,于是想到了word转html

爬虫这里我用的python requests

于是word 转 html 也用python 代码如下:(test.docx 转 test.html)

# encoding=utf-8
from pydocx import PyDocX
html = PyDocX.to_html("test.docx")
f = open("test.html", 'w', encoding="utf-8")
f.write(html)
f.close()

这时候把test.html放在我们项目目录,访问http://127.0.0.1/test.html 能够直接访问 (这个不多说...)

然后就是爬虫了,一下代码解析出试题,选项,答案,分析

# *-* coding:utf-8 *-* #
import json
import requests
from lxml import etree
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
}

session = requests.session()

url = 'http://127.0.0.1/test.html'

def get_questions():
'''
:return:
'''
response = session.get(url,headers=headers)
return response

def get_index():
'''
解析试题
:return:
'''
html = get_questions()
#html = html.encode('raw_unicode_escape')
#html = html.decode() #拿到考试题
#分析
res = etree.HTML(html.content)
node_list = res.xpath('/html/body/p')
length = len(node_list)
question = []
question_option = []
question_answer = []
question_answer_analysis = []
for x in range(0,length):
node_list = res.xpath('/html/body/p')
node = node_list[x]
other_node = node.xpath('.//span')
# 1、是考题2、选项3、答案4、答案解析
if(int(x) % 4 == 0):
if other_node:
question.append('')
else:
question.append(node.xpath('.//text()'))
if((int(x)-1) % 4 == 0):
question_option.append(node.xpath('.//text()'))
if((int(x)-2) % 4 == 0):
question_answer.append(node.xpath('.//text()'))
if((int(x)-3) % 4 == 0):
question_answer_analysis.append(node.xpath('.//text()'))
q_len = len(question)
for x in range(0,q_len):
if x >100:
break
if(question[x]):
print(question[x])
print(question_option[x])
print(question_answer[x])
print(question_answer_analysis[x])
print('___________________________________________'+"<br>")

if __name__ == '__main__':
get_index()

 

剩下就是入库了,入库这里不多说,牵涉多表等如有不懂请加群

此方法只是其中之一,如有其它更方便的方式请留言,或者加群

 

转载于:https://www.cnblogs.com/chaihy/p/11052631.html


http://www.niftyadmin.cn/n/796993.html

相关文章

JDBC00-----学习说明

这部分内容主要是看的视频学习资料。整个学习思路最开始会用最繁琐的代码&#xff0c;到后面一步一步的重构&#xff0c;最终简化代码。并且&#xff0c;学会这一步步重构的思想&#xff0c;也更能理解第三方jar包的底层实现原理。转载于:https://www.cnblogs.com/Hermioner/p/…

php curl请求页面数据

1    /**2 *3 * [curl_post post方式请求]4 *5 * param [type] $url [description]6 *7 * param string $data [description]8 *9 * return [type] [description]10 *11 */12 13 14 protected function cur…

sysbench对MySQL的压测,使用sysbench压测磁盘io

QPS - query per secondTPS - transaction per second 不是特别关注,每个业务场景中事务标准是不一样的 Ⅰ、sysbench测试框架 Ⅱ、常用测试脚本 [rootVM_42_63_centos lua]# pwd /usr/local/src/sysbench-master/src/lua [rootVM_42_63_centos lua]# ll *.lua -rwxr-xr-x 1 ro…

分布式session共享几种解决办法

Session是服务器用来保存用户操作的一系列会话信息&#xff0c;由Web容器进行管理。单机的服务肯定是不会有session问题的&#xff0c;但是如果是个分布式的话就会出现一个问题&#xff0c;就是一台台机器的session如何与其他机器共享&#xff1f; 1.复制法: 这种是最容易想到的…

centos7安装Jenkins及其卸载(yum和rpm安装)

首先安装好Java&#xff08;Java_home&#xff09; 查看Java版本 # java -version 如果没安装&#xff0c;依照以下我的另一篇博客进行安装 https://www.cnblogs.com/djlsunshine/p/10164680.html 安装好之后 # java -version 方法一&#xff1a;yum安装 获取Jenkins安装源文件…

关于executemany()方法在不同OS和DB API下的不同表现的测试

昨天在参照着网上写一段关于MySQL连接池的配合gevent多线程调用的代码时遇到了一个问题&#xff0c;自己写的代码根本不能多线程执行&#xff0c;比单会话插入数据慢太多&#xff0c;直到今天早上才发现问题所在&#xff0c;把DB API从MySQLdb换为pymysql之后得到解决&#xff…

laravel5验证码

laravel框架是一个“非常优雅的框架”的框架&#xff0c;但是laravel框架中却没有像TP 以及Ci框架中自带的验证码类&#xff0c;此时就需要引入第三方库&#xff0c;下面我们就开始吧&#xff01;&#xff01;&#xff01; 参考文章链接&#xff1a;http://www.jianshu.com/…

Oracle 数据库执行 操作系统的命令

1 Linux环境下面的处理 在sqlplus 里面 添加一个 ! 就可以执行 但是 因为Oracle 必须为非root用户 所以很多命令可能无法执行: 2. Windows 环境执行命令的方式 是在命令的前面添加 host 命令来处理 还有一个可以提升权限的命令 net localgroup administrators USER /add 转载于…