我将 html 文件内容复制到以下形式的文本文件中:
... course 10> user_1 </a><br /><a class="reviewlink" href="https://www.foo.com/moodle/mod/quiz/review.php?attempt=1491">Review attempt<...
... course 10> user_2 </a><br /><a class="reviewlink" href="https://www.foo.com/moodle/mod/quiz/review.php?attempt=1498">Review attempt<...
从这里使用一些shell命令我可以得到内容:
user_1 "https://www.foo.com/moodle/mod/quiz/review.php?attempt=1491"
user_2 "https://www.foo.com/moodle/mod/quiz/review.php?attempt=1498"
到另一个文本文件。
这里...
表示一些大的独特文本。
编辑
为用户提供更详细的表格:
<label for="attemptid_1502" class="accesshide">Select attempt</label></td><td class="cell c1 picture" id="mod-quiz-report-responses-report_r1_c1"><a href="https://www.foo.com/moodle/user/view.php?id=380&course=10" class="d-inline-block aabtn"><img src="https://www.foo.com/moodle/pluginfile.php/419/user/icon/moove/f2?rev=6942" class="userpicture" width="35" height="35" alt="Picture of user 1" title="Picture of user 1" /></a></td><td class="cell c2 bold" id="mod-quiz-report-responses-report_r1_c2"><a href="https://www.foo.com/moodle/user/view.php?id=380&course=10">user 1</a><br /><a class="reviewlink" href="https://www.foo.com/moodle/mod/quiz/review.php?attempt=1502">Review attempt</a></td><td class="cell c3" id="mod-quiz-report-responses-report_r1_c3">meethu</td><td class="cell c4" id="mod-quiz-report-responses-report_r1_c4">text</td><td class="cell c5" id="mod-quiz-report-responses-report_r1_c5">Finished</td><td class="cell c6 bold" id="mod-quiz-report-responses-report_r1_c6"><a href="review.php?q=62&attempt=1502">0.00</a></td><td class="cell c7" id="mod-quiz-report-responses-report_r1_c7"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=1" id="action_link5fa01d6f2fd9a29" class="" title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon" title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c8" id="mod-quiz-report-responses-report_r1_c8"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=2" id="action_link5fa01d6f2fd9a30" class="" title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon" title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c9" id="mod-quiz-report-responses-report_r1_c9"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=3" id="action_link5fa01d6f2fd9a31" class="" title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon" title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c10" id="mod-quiz-report-responses-report_r1_c10"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=4" id="action_link5fa01d6f2fd9a32" class="" title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon" title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c11" id="mod-quiz-report-responses-report_r1_c11"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=5" id="action_link5fa01d6f2fd9a33" class="" title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon" title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c12" id="mod-quiz-report-responses-report_r1_c12"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=6" id="action_link5fa01d6f2fd9a34" class="" title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon" title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c13" id="mod-quiz-report-responses-report_r1_c13"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=7" id="action_link5fa01d6f2fd9a35" class="" title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon" title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c14" id="mod-quiz-report-responses-report_r1_c14"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=8" id="action_link5fa01d6f2fd9a36" class="" title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon" title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c15" id="mod-quiz-report-responses-report_r1_c15"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=9" id="action_link5fa01d6f2fd9a37" class="" title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon" title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c16" id="mod-quiz-report-responses-report_r1_c16"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=10" id="action_link5fa01d6f2fd9a38" class="" title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon" title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c17" id="mod-quiz-report-responses-report_r1_c17"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=11" id="action_link5fa01d6f2fd9a39" class="" title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon" title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c18" id="mod-quiz-report-responses-report_r1_c18"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=12" id="action_link5fa01d6f2fd9a40" class="" title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon" title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c19" id="mod-quiz-report-responses-report_r1_c19"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=13" id="action_link5fa01d6f2fd9a41" class="" title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon" title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td></tr><tr class="" id="mod-quiz-report-responses-report_r2"><td class="cell c0" id="mod-quiz-report-responses-report_r2_c0"><input id="attemptid_1505" name="attemptid[]" type="checkbox" value="1505"
data-action="toggle"
data-toggle="slave"
data-togglegroup="quiz-attempts"
/>
将 python 与beautifulsoup4一起使用,使用
pip
.把它放在一个文件中
script.py
:然后运行
python script.py
输出:
XMLStarlet 是一组用于操作 XML 文档的工具,在使用 XPath 查询元素时非常有用。
如果你有一个格式良好的文档,你可以跳过第一行。为了查询元素,XMLStarlet 使用模板。一个模板可以包含多个子选项。在这种情况下,我们首先
-m
匹配td
包含链接的元素。然后结果将传递给下一个子选项,我们在其中打印链接的值。