1

我有以下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行格式是否正确...

4

1 回答 1

4

当您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

相反,请检查您的name属性attrs

soup.find(attrs={"name": "__RequestVerificationToken"})
于 2015-11-08T22:25:03.623 回答