无法访问python中带有请求的网页

2024-04-26 07:10:48 发布

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

在与我关于Unable to print links using beautifulsoup while automating through selenium的问题讨论之后

我意识到主要的问题是请求无法提取的URL。页面的URL实际上是https://society6.com/discover,但是我使用selenium登录到我的帐户,所以URL变成了https://society6.com/society?show=2

但是,我无法将第二个URL用于请求,因为它显示错误。如何从URL中删除这样的信息。你知道吗


Tags: tohttpscomurlseleniumlinksusingprint
1条回答
网友
1楼 · 发布于 2024-04-26 07:10:48

你需要先登录!你知道吗

为此,可以使用bs4.BeautifulSoup库。你知道吗

下面是我使用的一个实现:

import requests
from bs4 import BeautifulSoup

BASE_URL = "https://society6.com/"


def log_in_and_get_session():
    """
    Get the session object with login details
    :return: requests.Session
    """    
    ss = requests.Session()
    ss.verify = False    # optinal for uncertifaied sites. 
    text = ss.get(f"{BASE_URL}login").text
    csrf_token = BeautifulSoup(text, "html.parser").input["value"]
    data = {"username": "your_username", "password": "your_password", "csrfmiddlewaretoken": csrf_token}
    # results = ss.post("{}login".format(BASE_URL), data=data)
    results = ss.post("{}login".format(BASE_URL), data=data)
    if results.ok:
        print("Login success", results.status_code)
        return ss
    else:
        print("Can't  login", results.status_code)

正在使用“post”方法登录。。。你知道吗

希望这对你有帮助!你知道吗

编辑

添加了函数的开头。你知道吗

相关问题 更多 >