我是新加坡的一名16岁学生。由于 Covid-19,我无法在 9 月 11 日的计划日期参加 ACT。但是,由于考试中心已经关闭,下一个时段将在 12 月一直关闭,这对我真的很糟糕。但是,我知道通常会打开一个插槽,几个小时,直到有人认领它。因此,我想我可以在 Python 打开时向我发送电子邮件,这样我就可以领先一步。
当我找到我想参加考试的中心"数据库"时,我真的很高兴,链接 在我登录的默认浏览器上,它看起来像这样
我真的很兴奋,因为从那时起,我可以编写一个脚本,每小时检查一次数据 09..etc(九月(日期是否存在,然后给我发电子邮件。
但是,当我在 Python 中使用 Beautiful Soup 时,输出是需要身份验证的(。网站的身份验证是电子邮件和密码。谁能帮我如何使用美丽汤进行身份验证?谢谢!
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)
身份验证驻留在请求中。API 应指定需要对其进行身份验证和授权的标头。如果您找到正确的标题,您只需在请求中提供它们即可!或者更好的是尝试使用来自请求的基本身份验证
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)