【问题标题】:BeautifulSoup value of a tag returns None even though there is a value present and the tag was found python一个标签的 BeautifulSoup 值返回 None 即使存在一个值并且找到了标签 python
【发布时间】:2019-05-10 03:00:55
【问题描述】:

我正在尝试使用 find() 和 BeautifulSoup,但最近,当尝试查找某个标签的值时,即使该标签存在并且它包含一个值,也会返回 None。这是我的代码:

s = requests.Session()
checkout_session = s.get(cart_url, headers=headers)
print(checkout_session.url)
contact_info = s.get(checkout_session.url, headers=headers)
soup1 = BeautifulSoup(contact_info.text, features="lxml")
token1 = soup1.find("input", attrs={"name":"authenticity_token", "type":"hidden"})
print(token1.value)
print(token1)

当我运行代码时,标签值的第一次打印返回 none 但第二次打印返回标签 (<input name="authenticity_token" type="hidden" value="rwtWPTEwziwvOfWFXYTzniLewZxnJ/A2dWi9fgDwNg0FR53ty0AqiUNBYuhZY/PJJrnUues26SRj7LEcwradHg=="/>)

有谁知道我做错了什么?

【问题讨论】:

  • 能否添加html?

标签: python beautifulsoup web-crawler


【解决方案1】:

要获得value 属性调用它

print(token1['value'])
# or
print(token1.get('value'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2014-07-09
    相关资源
    最近更新 更多