在 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 中删除信息。
最佳答案
您需要先登录!
为此,您可以使用 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”方法登录...
希望这对您有帮助!
编辑
添加了函数的开头。
关于python - 无法通过python中的请求访问网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52352795/