不使用 requests.session() 登录



我使用请求库登录了一个网站。我尝试了刮擦,它工作正常。但是当我尝试使用请求时,它不起作用。登录后,我正在获取主页的内容,但没有获得页面的内容。当我在 post(( 方法之后打印 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"而不是"collegekart.in/users"的输出内容。

试一试。你做了很多不必要的事情来完成一个简单的任务。但是,当您登录时,您可以找到显示某些项目的网页。我也刮掉了标题。

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)

填充结果的部分输出:

Enriching Speakjng and Writing Skills
Engineering Chemistry 16th edition 
A Textbook of Engineering Physics
你可以

简单地使用 get(( 方法。在该使用参数属性作为登录凭据。

最新更新