未使用登录请求.会话()

2024-10-03 13:26:19 发布

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

我用请求库登录一个网站。我试过用刮痧,效果很好。但我不尝试处理请求。我得到的是主页的内容,而不是登录后的页面。当我获取url后,url()方法不正确。代码如下

    import requests
from bs4 import BeautifulSoup
r=requests.get("http://collegekart.in/login")
c=r.content
soup=BeautifulSoup(c,"html.parser")
token=soup.find("meta",{"name":"csrf-token"})
print(token)
tok=token['content']
print(tok)
s=requests.session()
login={"username":'fdgdgfdgdfgdfg@gmail.com',"password":'dgfdgdfgfdgdfgd',"csrf-token":tok}
s.post("http://collegekart.in/login",data=login)
t=s.get("http://collegekart.in/users")
print(t.url)
sop=BeautifulSoup(t.content,"html.parser")
print(sop.prettify())

我正在获取“的输出内容”大学生卡丁车“而不是”collegekart.in/users. 在


Tags: inimporttokenhttpurl内容getlogin
2条回答

试试看。为了完成一个简单的任务,你做了很多不必要的事情。但是,当您登录时,您可以找到显示一些项目的网页。我也勉强拿到了冠军。在

import requests
from bs4 import BeautifulSoup

payload={

'utf8':'✓',
'username':'zerqqr1@iydhp.com',
'password':'hanfenghanfeng'
}

res = requests.get("http://collegekart.in/access/attempt_login?",headers={'User-Agent':'Mozilla/5.0'},params=payload)
soup = BeautifulSoup(res.text,"lxml")
for item in soup.find_all(class_="title"):
    print(item.text)

填充结果的部分输出:

^{pr2}$

您可以简单地使用get()方法。在中,对登录凭据使用params属性。在

相关问题 更多 >