【问题标题】:Why doesn't Beautifulsoup find this input by name?为什么 Beautifulsoup 没有按名称找到这个输入?
【发布时间】:2016-02-09 13:00:32
【问题描述】:

我有以下Python 2.7.10 代码与RequestsBeautifulSoup4

print soup
RequestVerificationToken = soup.find(name="__RequestVerificationToken")
print RequestVerificationToken

print soup 打印我试图从中获取信息的网页。在输出中,打印的 HTML 包括以下内容:

<input name="__RequestVerificationToken" type="hidden" value="awbVKuhEwngnc6s6DYPxa0_paAaxyiSus_Gxx2KvZUdQjAAX5bx-icMZyIJJXiVjLniFz8t1YWrrehVZUWj2tGcgA6I1"/>

然而,RequestVerificationToken 被打印为None

我只想知道我的soup.find 行格式是否正确...

【问题讨论】:

    标签: python asp.net beautifulsoup python-requests roblox


    【解决方案1】:

    当您将 name 作为参数传递时 - 它被解释为 标记的名称,而 BeautifulSoup 将搜索 __RequestVerificationToken 元素。这是find() 方法的外观(请参阅第一个命名参数是name):

    def find(self, name=None, attrs={}, recursive=True, text=None,
             **kwargs):
        """Return only the first child of this Tag matching the given
        criteria."""
        r = None
        l = self.find_all(name, attrs, recursive, text, 1, **kwargs)
        if l:
            r = l[0]
        return r
    

    请检查attrs 中的name 属性:

    soup.find(attrs={"name": "__RequestVerificationToken"})
    

    【讨论】:

    • 谢谢!我还需要获得它的价值,但是在您提供的行之后添加 ['value']
    猜你喜欢
    • 2018-05-11
    • 1970-01-01
    • 2023-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多