Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 美丽的汤从一个问题的多个答案_Python_Beautifulsoup - Fatal编程技术网

Python 美丽的汤从一个问题的多个答案

Python 美丽的汤从一个问题的多个答案,python,beautifulsoup,Python,Beautifulsoup,我正在尝试用漂亮的汤拼凑一个有多个答案的html测验。我能够独立地拼凑答案和问题,并且已经找到了如何将其中一些结合起来的方法,下面是错误。问题是有些问题有不止一个答案,因此我在输出上偏离了一个,即有多个答案的问题1将只有一个答案,然后问题2将有问题1的答案。html代码中有很多层,即复选框、正确标记和错误标记答案。。。对于下一个问题,这会以相同的顺序重复。最终状态是刮取正确的问题和答案,然后将其转储到闪存卡程序中以供学习之用。我不确定这是不是最好的方法,或者是否有另一个python库工作得更好?

我正在尝试用漂亮的汤拼凑一个有多个答案的html测验。我能够独立地拼凑答案和问题,并且已经找到了如何将其中一些结合起来的方法,下面是错误。问题是有些问题有不止一个答案,因此我在输出上偏离了一个,即有多个答案的问题1将只有一个答案,然后问题2将有问题1的答案。html代码中有很多层,即复选框、正确标记和错误标记答案。。。对于下一个问题,这会以相同的顺序重复。最终状态是刮取正确的问题和答案,然后将其转储到闪存卡程序中以供学习之用。我不确定这是不是最好的方法,或者是否有另一个python库工作得更好?任何指导和帮助都将不胜感激

问题1:

问题1:
不正确

选择两个XYZ示例。

  • 答复1

    (不正确)
  • ​ 答复2

  • ​ 答复3

    (正确)
  • ​ XYZ管理

    (正确)
  • 答复4

代码:
从bs4导入美化组
进口稀土
将open('inputfile.html',encoding=“utf-8”)作为f:
contents=f.read()
汤=美汤(内容为“lxml”)
questions=soup.find_all('div','mc-quick-question--question prompt--2_dlz'))
answers=soup.find_all('div','fx-fx-lc-mc-quick-answer--correct--is6Db','safe-set-inner-html:rich-text-viewer:html')
对于zip中的q、a(问题、答案):
打印(“”.join((q.p.text,a.p.text)))
我的输出: 错误 这将解析大约一半的错误,假设它需要文本类型并返回其他内容

回溯(最近一次呼叫最后一次):
文件“myparsar.py”,第111行,在
打印(“”.join((q.p.text,a.p.text)))
AttributeError:“非类型”对象没有属性“文本”

好的,如果我理解正确,您希望从一个问题中获得多个正确答案。你做得基本正确。但是在for循环中,您错误地使用了zip函数。我建议你检查一下

如果您有多个问题,应开始按容器查找所有问题:

question_containers = soup.find_all('div', 'detailed-result-panel--question-container--7NyiS')
然后,您可以遍历每个容器以获得问题和答案

for container in question_containers:
    question = container.find('div', 'mc-quiz-question--question-prompt--2_dlz')
    answers = container.find_all('div', 'fx fx-lc mc-quiz-answer--correct--is6Db', 'safely-set-inner-html:rich-text-viewer:html')
由于一个问题有多个答案,您应该使用嵌套for循环来迭代所有答案:

ans = []
for a in answers:
    answer_text = a.text.strip().split('\n')
    ans.append(answer_text[0])
print('Question:\n', question.text.strip(), '\nAnswers:\n', ', '.join(ans))
对于每个答案,我使用.text、.strip和.split仅获取答案文本。然后我把它附加到一个答案列表中,然后我用这个列表打印出你想要的

完整代码:
请用预期输出编辑您的问题。谢谢您的回复!是的,这就是我想要实现的。我想我看到了一条更好的路径,但代码实际上填充了所有答案#输出:“问题:选择两个XYZ示例。回答:回答3,回答4,回答3问题:XYZ的一个例子是什么。答案:答案3,答案4,答案3’这是因为你在一次搜索中得到了所有答案。我建议您首先查找所有问题容器。然后在每个容器中查找问题和正确答案。我会根据您的要求编辑答案。谢谢!这在很大程度上起了作用。由于某些原因,在较大的数据集中,它正在抓取
(正确)
,因此我添加了一个替换来删除它,并且还使用了一个返回,而不是换行符
answer\u text=a.text.strip().replace('(正确)',)。split('\r')
我尝试使用beautifulsou进行筛选以删除它,但没有成功。我现在可以抓取文本并向anki flashcards写下。
for container in question_containers:
    question = container.find('div', 'mc-quiz-question--question-prompt--2_dlz')
    answers = container.find_all('div', 'fx fx-lc mc-quiz-answer--correct--is6Db', 'safely-set-inner-html:rich-text-viewer:html')
ans = []
for a in answers:
    answer_text = a.text.strip().split('\n')
    ans.append(answer_text[0])
print('Question:\n', question.text.strip(), '\nAnswers:\n', ', '.join(ans))
soup = BeautifulSoup(contents, 'lxml')

question_containers = soup.find_all('div', 'detailed-result-panel--question-container--7NyiS')
for container in question_containers:
    question = container.find('div', 'mc-quiz-question--question-prompt--2_dlz')
    answers = container.find_all('div', 'fx fx-lc mc-quiz-answer--correct--is6Db', 'safely-set-inner-html:rich-text-viewer:html')

    ans = []
    for a in answers:
        answer_text = a.text.strip().split('\n')
        ans.append(answer_text[0])
    print('Question:\n', question.text.strip(), '\nAnswers:\n', ', '.join(ans))