我正在使用此 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 refrom bs4 import BeautifulSoupwith open("index.html") as fp: soup = BeautifulSoup(fp, "lxml")for script in soup.find_all(re.compile("(?<=alert\(\").+(?=\")")): print(script)这不会返回任何东西。这是基于 BS 文档中“正则表达式”下给出的示例,用于查找以“b”开头的标签名称:import refor 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)这将返回“无”。
1 回答
慕森卡
TA贡献1806条经验 获得超8个赞
运行所有html数据将不起作用。首先,您需要提取script数据,然后才能轻松解析alert文本。
import re
from bs4 import BeautifulSoup
with open("index.html") as fp:
soup = BeautifulSoup(fp, "lxml")
script = soup.find("script").extract()
# find all alert text
alert = re.findall(r'(?<=alert\(\").+(?=\")', script.text)
print(alert)
输出:
['Please fill all the required fields.']
添加回答
举报
0/150
提交
取消