Epacity
Epacity

Reputation: 97

BeautifulSoup value of a tag returns None even though there is a value present and the tag was found python

I'm experimenting with find() and BeautifulSoup but recently, when trying to find the value of a certain tag, None is returned even though the tag is present and it contains a value. Here is my code:

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)

When I run the code, the first print for the tag's value returns none yet the second print returns the tag (<input name="authenticity_token" type="hidden" value="rwtWPTEwziwvOfWFXYTzniLewZxnJ/A2dWi9fgDwNg0FR53ty0AqiUNBYuhZY/PJJrnUues26SRj7LEcwradHg=="/>)

Does anyone know what I am doing wrong?

Upvotes: 2

Views: 237

Answers (1)

ewwink
ewwink

Reputation: 19154

to get value attribute call it with

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

Upvotes: 2

Related Questions