如何在BeautifulSoup中进行身份验证?输出所需的身份验证

2024-10-03 21:27:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一名16岁的新加坡学生。由于新冠肺炎,我无法在计划的9月11日采取行动。然而,由于考试中心已经关闭了,下一个时段将在12月份开始,这对我来说真的很糟糕。然而,我知道经常有一个插槽打开,持续几个小时,直到有人认领它。因此,我想我可以使用Python在它打开时给我发送一封电子邮件,这样我就可以走在前面

当我找到我想参加考试的中心的“数据库”时,我真的很高兴 在我已登录的默认浏览器上,它看起来像this

我真的很兴奋,因为从那以后,我可以写一个脚本,每小时检查数据09..etc(9月)日期是否存在,并给我发电子邮件

但是,当我在Python中使用Beautiful Soup时,输出是必需的)。网站的身份验证是电子邮件和密码。有谁能帮助我如何使用BeautifulSoup进行身份验证?谢谢

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)


Tags: testimport身份验证parserurl电子邮件中心requests
1条回答
网友
1楼 · 发布于 2024-10-03 21:27:22

身份验证驻留在请求中。api应该指定需要对哪些头进行身份验证和授权。如果您找到了正确的标题,您可以在请求中简单地提供它们!或者最好尝试使用来自请求的Basic authentication

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)

相关问题 更多 >