使用Requests Python登录到网站(9anime)

问题描述 投票:-1回答:3

解决方案:此特定站点的actionaction="user/ajax/login",因此必须将其附加到主站点的url上以实现有效负载。 (可以通过在action中的ctrl + f中搜索action来找到Page Source)。 url是要刮除的内容。 with requests.Session() as s:是从站点内部维护cookie的基础,这使刮擦过程保持一致。 res变量是将有效负载发布到登录URL中的响应,允许用户从特定的帐户页面抓取。发布后,请求将达到指定的url。有了此功能,BeautifulSoup现在可以从帐户站点中获取并解析HTML。在这种情况下,"html.parser""lxml"都兼容。如果iframe中有HTML,则可以仅使用requests来捕获和解析HTML,这是令人怀疑的,因此我建议最好使用selenium,最好是使用Firefox。

import requests

payload = {"username":"?????", "password":"?????"}
url = "https://9anime.to/user/watchlist"
loginurl = "https://9anime.to/user/ajax/login"

with requests.Session() as s:
    res = s.post(loginurl, data=payload)
    res = s.get(url)
from bs4 import BeautifulSoup

soup = BeautifulSoup(res.text, "html.parser")

[[Windows 10]要安装Selenium pip3 install selenium和驱动程序-(chrome:https://sites.google.com/a/chromium.org/chromedriver/downloads)(Firefox:https://github.com/mozilla/geckodriver/releases)如何将“ geckodriver”放入Firefox Selenium的PATH中:control panel"environmental variables[ C0]"Path""New" "file location for "geckodriver"然后,一切就绪。另外,为了在使用硒时获取enter,请在使用驱动程序“获取” URL后尝试iframesimport time。这将使网站有更多时间来加载这些额外的time.sleep(5)示例:

iframes
python selenium-webdriver beautifulsoup python-requests
3个回答
2
投票

您正在尝试向需要登录的URL发出import time from bs4 import BeautifulSoup from selenium import webdriver driver = webdriver.Firefox() # The WebDriver for this script driver.get("https://www.google.com/") time.sleep(5) # Extra time for the iframe(s) to load soup = BeautifulSoup(driver.page_source, "lxml") print(soup.prettify()) # To see full HTML content print(soup.find_all("iframe")) # Finds all iframes print(soup.find("iframe"))["src"] # If you need the 'src' from within an iframe. 请求,因此,它会产生GET错误,表示已禁止。这意味着该请求未通过身份验证才能查看内容。

如果您根据在403请求中构建的URL来考虑,您将在URL中从字面上公开用户名GET和密码(x),如下所示:

(y)

...这当然是安全隐患。

[在不知道您对该特定站点具有什么特定访问权限的情况下,原则上,您首先需要使用https://9anime.to/user/watchlist?username=x&password=y请求模拟身份验证,然后再在该页面上执行POST请求。成功的响应将返回GET状态代码200,然后您就可以使用BeautifulSoup解析内容并从相关HTML标记之间定位该内容的所需部分。


1
投票

我建议,首先提供登录页面的地址并连接。然后你做一个

('OK')

为了允许您暂停连接时间(一旦连接并确认,您必须在终端上按ENTER键才能继续该过程。)


0
投票

已解决:在这种情况下,input('Enter something')action-tag。因此,通过将其附加到网站的原始主URL上,而不是user/ajax/login而不是https://9anime.to/user/watchlist,您将获得https://9anime.to,这将为您提供登录URL。

https://9anime.to/user/ajax/login
© www.soinside.com 2019 - 2024. All rights reserved.