How do I authenticate in BeautifulSoup? Authentication Required is Output

Viewed 240

I am a 16 year old student in Singapore. Due to Covid-19, I am unable to take the ACT at the planned date being September 11. However, since test centers have closed down the next slot is all the way in December, making it really bad for me. However, I know that often one slot opens up, for a few hours until someone claims it. Hence, I thought I could use Python to send me an email when it opens up so I could be ahead of the curve.

I was really happy when I found the 'database' for the center where I want to take the test, link On my default browser, where I have signed in, it looks like this

I was really excited since from that, I could write a script that would check every hour if the data 09..etc (September) date was present, and email me.

However, when I use Beautiful Soup in Python, the output is authentication required). The authentication for the website is email and password. Could anyone help me as to how to authenticate using BeautifulSoup? Thanks!

import requests 
from bs4 import BeautifulSoup 

url='https://my.act.org/api/test-scheduling/ACTInternational/test-dates/YTLQVQEZ?roomType=REGULAR&testCenterId=YTLQVQEZ'

    #open with GET method 
resp=requests.get(url)   
        # we need a parser,Python built-in HTML parser is enough . 
soup=BeautifulSoup(resp.text,'html.parser')     
print(soup)

1 Answers

The authentication resides in the request. The api should specify which headers its need to be authenticated and authorized. If you find the right headers you can simply provide them in your request ! Or better yet try and use the Basic authentication from requests

import requests 
from requests.auth import HTTPBasicAuth
from bs4 import BeautifulSoup 

url='https://my.act.org/api/test-scheduling/ACTInternational/test-dates/YTLQVQEZ?roomType=REGULAR&testCenterId=YTLQVQEZ'

#open with GET method 
#We can now provide credentials, in your case email and pass
resp=requests.get(url, auth=HTTPBasicAuth('user', 'pass'))   
        # we need a parser,Python built-in HTML parser is enough . 
soup=BeautifulSoup(resp.text,'html.parser')     
print(soup)
Related