Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 未使用请求登录。会话()_Python_Session_Web Scraping_Beautifulsoup_Python Requests - Fatal编程技术网

Python 未使用请求登录。会话()

Python 未使用请求登录。会话(),python,session,web-scraping,beautifulsoup,python-requests,Python,Session,Web Scraping,Beautifulsoup,Python Requests,我使用请求库登录到一个网站。我试过用scrapy,效果很好。但是当我尝试请求时,它不起作用。我得到了主页的内容,但不是登录后的页面。当我在post()方法之后打印url时,我没有得到正确的url。代码如下 import requests from bs4 import BeautifulSoup r=requests.get("http://collegekart.in/login") c=r.content soup=BeautifulSoup(c,"html.parser") tok

我使用请求库登录到一个网站。我试过用scrapy,效果很好。但是当我尝试请求时,它不起作用。我得到了主页的内容,但不是登录后的页面。当我在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()方法。在这种情况下,使用params属性作为登录凭据。

密码:'hanfenghanfeng','username':'zerqqr1@iydhp.com'使用这些凭据。那么collegekart.in/users将起作用。为什么post()方法不起作用?post()是为了这个权利。在做这些事情之前,你应该对任何网页的响应有一个全面的了解,你可以使用任何开发工具来观察。但是,我认为我的答案符合你的目的,所以不要忘记接受它作为一个答案。谢谢。打开任何网页,然后右击它,你会注意到一些选项,包括spect元素。选择该元素后,您将注意到不同的选项卡,其中选择
网络选项卡
,然后选择
xhr
选项卡,有时选择
全部
选项卡。然后重新加载页面并查看活动。谢谢。谢谢您的帮助!