使用BeautifulSoup和requests如何爬取受密码保护的网站

2024-10-01 07:27:52 发布

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

我试图访问一个受密码登录(用户名和密码)保护的html页面。我试过使用请求,但似乎不起作用。在

import r
import requests
def Login():

    scrape_url = 'https://www.ecoledirecte.com/Eleves/3668/Notes'

    login_url = 'https://www.ecoledirecte.com/login'

    payload = {
        'username': '***',
        'password': '***'
    }


    with requests.Session() as session:
        post = session.post(login_url, data=payload)
        r = session.get(scrape_url)
        print(r.text)

我没有发现任何错误,但是print(r.text)只给出登录页面的html代码,而不是scrape_url。在


Tags: httpsimportcomurl密码sessionhtmlwww
1条回答
网友
1楼 · 发布于 2024-10-01 07:27:52

当你访问网站时,你需要模仿你的浏览器在做什么。如果您尝试在浏览器的开发工具(instructions for Chrome)中登录并打开网络监视器,您会看到位于https://www.ecoledirecte.com/login的页面只包含表单,这不是您要发布到的URL。在

相反,当您输入用户名时,浏览器会向https://api.ecoledirecte.com/v3/login.awp发送一个POST请求,其中包含

data={
    "identifiant": "username",
    "motdepasse": "pass"
}

因此,您应该将登录代码更改为:

^{pr2}$

相关问题 更多 >