Home > other >  Use BeautifulSoup to return attribute
Use BeautifulSoup to return attribute

Time:04-05

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

CodePudding user response:

Add headers and there you have it:

import requests
from bs4 import BeautifulSoup as bs

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0",
}

url = 'https://services.paci.gov.kw/card/inquiry?lang=ar&serviceType=2'
soup = bs(requests.get(url, headers=headers).text, 'html.parser')
vToken = soup.find('input', attrs={'name': '__RequestVerificationToken'})
print(vToken['value'])

Output:

CfDJ8KYexE-JuUdFv8XKqnZO6wgPM1_Pmaf96y1YdvqHY2mJLJtbNiU0p4tU3xVQSudgrpBxQmINXC8dopZp6NEb3rl-GRhpmSfDrUwn8_uGnNKB6u7cHDNNNIXo-bGEH0gNz6nq4IIWfIgdDqQxl47E6vQ

CodePudding user response:

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
  • Related