Python 获取随机生成数学计算内容的代码

Python code to get the content of randomly generated math calculation

我有一个生成随机方程的网络应用程序,我正在尝试使用 Python:

自动求解它们
Can you solve the level 1?<br/><h3><div id='calc'>0 * 8</div></h3><br/>
<form action="play.php" method="post">
 <input type="text" name="res" />
 <input type="submit" value="OK">
</form>
</div>
</body>

我写了一个脚本来获取每次生成的方程式:

number =  re.findall('<div id='calc'> (.*)</div></h3><br/>', content)[0]

但由于某种原因它不断抛出错误

    number =  re.findall('<div id='calc'> (.*)</div></h3><br/>', content)[0]
                                      ^
SyntaxError: invalid syntax

以前有人遇到过这个吗?

number =  re.findall(r"<div id='calc'> (.*)</div></h3><br/>", content)[0]

这应该有效,您使用单引号 ' 来定义字符串并且还在字符串中使用未转义的单引号,这导致字符串被解释为 <div id=

在python中,r"someString"表示原始字符串,在使用正则表达式搜索时更推荐使用它们。您可以阅读有关原始字符串的更多信息 here