Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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
使用BeautifulSoup捕获JavaScript警报文本_Javascript_Python_Beautifulsoup - Fatal编程技术网

使用BeautifulSoup捕获JavaScript警报文本

使用BeautifulSoup捕获JavaScript警报文本,javascript,python,beautifulsoup,Javascript,Python,Beautifulsoup,我正在使用此JavaScript验证表单: <script type="text/javascript"> function validateForm() { var a=document.forms["orderform"]["Name"].value; var b=document.forms["orderform"]["Street"].value; var c=docume

我正在使用此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>
但我似乎找不到与“print(tag.name)”相同的方法来打印警报文本。还是我完全走错了方向?非常感谢您的帮助

编辑: 我试过:


pattern=re.compile((?如果我理解正确,这可能就是您要找的:

html = """
 <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=="", a==null || c=="", c==null || d=="", d==null || e=="", a==null || e=="")
        {alert("Please fill all the required fields.");
        return false;
        }
    }
  </script>
   """

soup = BeautifulSoup(html, "lxml")
alert = soup.text.split('"')
alert[33] 

运行所有
html
数据将不起作用。首先需要提取
脚本
数据,然后可以轻松解析
警报
文本

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)

以B开头的标记是什么意思?文档中的示例与一行(^B)开头的字母“B”匹配,因此返回标记名“body”和“B”。我希望根据自己的目的调整此示例,但暂时没有效果。
中括号前的转义(?顺便说一句,
a==null | a==“”,b==null | | b==“”,a==null | | c=“”,c==null | | d=“”,d==null | | e=“”,a==null | | e=“”
意味着整个测试等同于
a==null | e=“”
。逗号与JS中的
|
非常不同:@MikeSamuel感谢您查看此JavaScript。看起来我完全搞砸了,不仅仅是因为逗号。我在帖子中修复了它。这很有效。感谢您的解释和代码。在我的情况下,我得到了“[u'请填写所有必填字段”。],我需要从我的输出中删除此Unicode标记和括号。但我发现这是其他人以前在这里提出的问题,我会找到答案的。@cbp很高兴能为您提供帮助。对于单个警报
消息
,您可以尝试
打印(警报[0])
太好了!再次感谢。顺便说一句,我正试图更新你的答案,但系统不允许我。声誉还不够…@cbp我明白了。系统可能有问题。无论如何,你可以批准答案。
pattern = re.compile("(?<=alert\(\").+(?=\")"))
for script in soup.find_all ('script'):
  print(script.pattern)
html = """
 <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=="", a==null || c=="", c==null || d=="", d==null || e=="", a==null || e=="")
        {alert("Please fill all the required fields.");
        return false;
        }
    }
  </script>
   """

soup = BeautifulSoup(html, "lxml")
alert = soup.text.split('"')
alert[33] 
'Please fill all the required fields.'
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.']