Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use BeautifulSoup to return attribute

I am trying to get an attribute of an element but I got None for that element
Here’s my code

import requests
from bs4 import BeautifulSoup as bs

res = requests.get('https://services.paci.gov.kw/card/inquiry?lang=ar&serviceType=2')
#print(res.text)
with open('Output.txt', 'w') as f:
    f.write(str(res.text))


soup = bs(res.text, 'html.parser')
vToken = soup.find('input', attrs={'name': '__RequestVerificationToken'}, first=True)
print(vToken)
print(vToken['value'])

I checked the Output.txt file and I found that attribute implemented but couldn’t get its attribute

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Skip the first=True argument from your find(), it will throw:

 TypeError: 'NoneType' object is not subscriptable

Example:

import requests
from bs4 import BeautifulSoup as bs

res = requests.get('https://services.paci.gov.kw/card/inquiry?lang=ar&serviceType=2')

soup = bs(res.text, 'html.parser')
vToken = soup.find('input', attrs={'name': '__RequestVerificationToken'})

print(vToken['value'])

Output:

CfDJ8Jg8_Y_lKLtGsMsCG9ry3AgPF3n0c5Zc7jzzJ0hYmIv2my6IqtFlABkZLpwb3f9JfCP3-Yhr_P4bvwp_jyw47G6MTcAAUx9ZBE82oSQ4tpSrJxS4vxtJj6LPZ2vaLwGDgiYitq3qYiJmNZvsg_FHOj0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading