gpt4 book ai didi

python - 为什么 Beautifulsoup 找不到这个输入的名称?

转载 作者:行者123 更新时间:2023-12-01 04:21:47 25 4
gpt4 key购买 nike

我有以下带有 RequestsBeautifulSoup4Python 2.7.10 代码:

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 行的格式是否正确...

最佳答案

当您将 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"})

关于python - 为什么 Beautifulsoup 找不到这个输入的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33599691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com