【问题标题】:Capture JavaScript alert text using BeautifulSoup使用 BeautifulSoup 捕获 JavaScript 警报文本
【发布时间】: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”。我希望根据自己的目的调整这个示例,但目前无济于事。
  • "(?&lt;=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


【解决方案1】:

运行所有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.']

【讨论】:

  • 这行得通。感谢您的解释和代码。在我的情况下,我得到'[u'请填写所有必填字段。']',我需要从我的输出中获取这个 Unicode 标记和括号。但我知道这是其他人之前已经在这里问过的问题,我会找出答案的。
  • @cbp 很高兴它帮助你。对于单个警报message,您可以尝试print(alert[0])
  • 完美!再次感谢。
  • 顺便说一句,我想支持你的答案,但系统不允许。还没有足够的声望...
  • @cbp 我明白了。可能是系统有问题。无论如何,您可以批准答案。
【解决方案2】:

如果我对您的理解正确,这可能就是您要查找的内容:

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.'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 2014-03-25
    • 2020-07-09
    • 2017-08-14
    • 2018-05-12
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    相关资源
    最近更新 更多