首页 > 其他分享 >挑战答题题库爬取

挑战答题题库爬取

时间:2022-11-17 10:12:50浏览次数:46  
标签:content 答题 temp arrays 爬取 cursor print 题库 options


可参考思路

挑战答题题库爬取_python


挑战答题题库爬取_题库_02


挑战答题题库爬取_html_03

# coding:utf-8
import requests
import pymysql
from bs4 import BeautifulSoup
import time
from lxml import etree
import re

class Bank:
def __init__(self):
pass

# 抓取平台1
def getQuestionBank(self):
url = 'http://www.bsmz.net/gongwen/805561.html'
headers = {
'Referer': 'http://xfxuezhang.cn/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'
}

html = requests.get(url, headers=headers).content.decode('gbk')
tree = etree.HTML(html)
content = tree.xpath('//div[@class="clearfix neirong"]/p/text()')
# print(content)
return content

# 数据清洗
def parseQuestions(self, content):
# 最终的结果集
arrays = []
# 临时工具数组
temp = []
# 存选项
options = []
# 跳过,“选择词语的正确词形”这个题型以后再说
skip = 0
for i in content:
i = i.replace('\u3000', '')
# 答案
if i.startswith('参考答案'):
if skip == 1:
skip = 0
continue
# print(i+" "+str(temp))
# 选项对应的数组下标
if len(re.findall(r'[a-eA-E]', i)) == 0:
continue
index = ord(re.findall(r'[a-eA-E]', i)[0].upper()) - 65
# 选项对应的答案内容
answer = options.pop(index)
# print(str(len(temp))+' '+str(temp)+' '+answer)
temp.append(re.findall(r'[a-eA-E]、{0,}(.+)', answer)[0])
# 存入结果集
arrays.append(temp.copy())
# print(temp)
# 数组清空,为下次做准备
temp.clear()
# 数组清空,为下次做准备
options.clear()
# 选项
elif re.match(r'^[A-E]+', i):
if skip == 1:
continue
options.append(i)
# 题干
else:
# 这个题型以后再说
if '选择词语的正确词形' in i:
skip = 1
continue
# 去除特殊字符,并加入
temp.append((temp.pop() if len(temp) != 0 else '')+re.sub('\W+', '', i).replace("_", ''))
# print(arrays)
# print(len(arrays))
return arrays

# 数据持久化
def storeQuestions(self, arrays):
db = pymysql.connect(host="localhost", port=3306, user="root", passwd="1061700625", db="learn")
cursor = db.cursor(pymysql.cursors.DictCursor) # 使用cursor()方法获取操作游标,按字典返回
for item in arrays:
print('*' * 20)
print(item)
if len(item) < 2:
print(">> 数据错误")
continue
question = item[0]
options = None
answer = item[1]
sql = "select * from bank where question='{}'".format(question)
cursor.execute(sql)
data = cursor.fetchall()
if len(data) != 0:
print(">> 数据存在")
continue
sql = "insert into bank(question,options,answer) values('{}','{}','{}')".format(question, options, answer)
try:
cursor.execute(sql) # 使用execute方法执行SQL语句
db.commit() # 提交到数据库执行
print(">> 插入完成")
except Exception as e:
print(">> 插入失败")
print('*' * 20 + '\r\n' + str(e) + '\r\n' + '*' * 20)
db.rollback() # 发生错误时回滚
cursor.close()
db.close()


if __name__ == '__main__':
bank = Bank()
content = bank.getQuestionBank()
arrays = bank.parseQuestions(content)
bank.storeQuestions(arrays)


标签:content,答题,temp,arrays,爬取,cursor,print,题库,options
From: https://blog.51cto.com/xfxuezhang/5859807

相关文章

  • python爬取公众号文章发布时间
    使用xpath取出来的是空,爬取到本地的html,时间的标签如下,内容也是是空的<emid="publish_time"class="rich_media_metarich_media_meta_text"></em>经过查找发现网页使用的......
  • python爬取斗鱼主播图片
         今天闲来无事,爬取一下斗鱼女主播的图片,之前学习scrapy的时候写过一个找不到了,今天使用requests和bs4重新写了一份,闲话不多说,直奔主题。首先用Chrome浏览器......
  • python爬取智联招聘信息_F_hawk189_新浪博客
    分享今天写的一个爬取智联招聘信息的爬虫,使用了requests和re模块,没有写注释,但是代码都比较简单,不是太难,这是爬取的信息:​​​​​​以下是源码部分:复制过来又没......
  • python学习 爬取亚马逊网页,失败后。修改HTTP报文头部后成功!
    通过修改HTTP报文头部,来成功获取网页内容!  pythonimportrequestsr=requests.get("https://www.amazon.cn/gp/product/B01M8L5Z3Y")r.status_coder.encoding  >>>......
  • 小爬爬6: 网易新闻scrapy+selenium的爬取
    1.​​https://news.163.com/​​国内国际,军事航空,无人机都是动态加载的,先不管其他我们最后再搞中间件2.我们可以查看到"国内"等板块的位置  新建一个项目,创建一个爬......
  • 爬取彼岸网明星图片
    爬取彼岸网明星图片:背景:彼岸网是一个大型的图片网站,上面有很多的图片,这次我们就来爬取彼岸网第一步:准备Python爬取准备前的各种库:importrequestsfromlxmlimportetreeimp......
  • 正则爬取糗事百科热图
    正则爬取糗事百科热图:第一步:找到网址我们分析Header,是Get请求下面开始撸代码:importrequestsimportjson,timeimportre,os上面先导入库没有的pipintstall安装库,可以通过......
  • 基于微信小程序的知识题库系统设计与实现-计算机毕业设计源码+LW文档
    小程序开发说明开发语言:Java框架:ssmJDK版本:JDK1.8服务器:tomcat7数据库:mysql5.7(一定要5.7版本)数据库工具:Navicat11开发软件:eclipse/myeclipse/ideaMaven包:Maven3.3.9......
  • Python爬虫之简单爬虫之爬取英雄联盟官网的英雄的皮肤
    Python爬虫之简单爬虫之爬取英雄联盟官网的英雄的皮肤文章目录​​Python爬虫之简单爬虫之爬取英雄联盟官网的英雄的皮肤​​​​背景:LOL这款游戏有着大量的玩家,这个游戏里......
  • Python爬虫之爬取绝对领域美女图片
    Python爬虫之爬取绝对领域美女图片第一步:导入模块:importrequestsfromlxmlimportetree第二步:定义函数:defget_url(start_url):response=requests.get(start_url)d......