【发布时间】:2019-07-23 17:01:13
【问题描述】:
我正在使用这个 JavaScript 来验证表单:
<script type="text/javascript">
function validateForm()
{
var a=document.forms["orderform"]["Name"].value;
var b=document.forms["orderform"]["Street"].value;
var c=document.forms["orderform"]["ZIP"].value;
var d=document.forms["orderform"]["City"].value;
var e=document.forms["orderform"]["PhoneNumber"].value;
if (
a==null || a=="" ||
b==null || b=="" ||
c==null || c=="" ||
d==null || d=="" ||
e==null || e==""
)
{alert("Please fill all the required fields.");
return false;
}
}
</script>
我正在尝试使用 BeatifulSoup 捕获警报文本:
import re
from bs4 import BeautifulSoup
with open("index.html") as fp:
soup = BeautifulSoup(fp, "lxml")
for script in soup.find_all(re.compile("(?<=alert\(\").+(?=\")")):
print(script)
这不会返回任何东西。这是基于“A 正则表达式”下的 BS 文档中给出的示例,以查找以“b”开头的标记名称:
import re
for tag in soup.find_all(re.compile("^b")):
print(tag.name)
# body
# b
但我似乎无法找到可以打印警报文本的“print(tag.name)”的等效项。还是我完全走错了路?非常感谢任何帮助。
编辑: 我试过了:
pattern = re.compile("(?<=alert\(\").+(?=\")"))
for script in soup.find_all ('script'):
print(script.pattern)
这将返回“无”。
【问题讨论】:
-
B开头的标签是什么意思?
-
文档中的示例匹配行首的字母“b” (^b),因此返回标签名称“body”和“b”。我希望根据自己的目的调整这个示例,但目前无济于事。
-
"(?<=alert\(\").+(?=\")"中括号前的转义无效。也许使用r"..."字符串。 -
顺便说一句,
a==null || a=="", b==null || b=="", a==null || c=="", c==null || d=="", d==null || e=="", a==null || e==""中的逗号表示整个测试等价于a==null || e==""。逗号与 JS 中的||有很大不同:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… -
@MikeSamuel 感谢您查看此 JavaScript。看起来我完全搞砸了,不仅仅是逗号。我在帖子中修复了它。
标签: javascript python beautifulsoup