我一直在尝试使用pythons请求模块登录https://angel.co/users/login。问题是,每次运行代码时,我都会收到404状态代码,即使我知道url存在。 我认为问题是url中的表单有一个带有相对链接的action属性。 我不知道如何解决这个问题,我没有找到解决方案的运气。
以下是我正在使用的代码:
import requests
with requests.Session() as s:
url = 'https://angel.co/users/login'
payload = { 'user[email]' : 'username_here',
'user[password]' : 'password_here'}
r = s.post(url, data=payload)
print r.status_code # This is printing 404
以下是我最终使用的代码:
import requests
from bs4 import BeautifulSoup
from login_details import username, password # This is just a script with my username and password
s=requests.session()
main_url="https://angel.co/login?utm_source=top_nav_home"
s.headers = {'Content-Type' : 'application/x-www-form-urlencoded',
'Host' : 'angel.co',
'Origin' : 'https://angel.co',
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64)' \
'AppleWebKit/537.36 (KHTML, like Gecko) ' \
'Chrome/44.0.2403.157 ' \
'Safari/537.36',
'Referer' : main_url,
'Upgrade-Insecure-Requests' : '1'}
response = s.get(main_url)
soup = BeautifulSoup(response.content)
payload={'login_only' : 'true',
'user[email]' : username,
'user[password]' : password,
'authenticity_token' : soup.find(attrs={'name' : 'authenticity_token'})['value'],
'utf8' : '%25E2%259C%2593'}
#the value of utf8 gets urlencoded once you send the request.
response = s.post("https://angel.co/users/login", data = payload)
print response.status_code
感谢Thothadri和Blackjack。
答案 0 :(得分:1)
import re,requests
s=requests.session()
main_url="https://angel.co/login?utm_source=top_nav_home"
headers={"Content-Type":"application/x-www-form-urlencoded","Host":"angel.co","Origin":"https://angel.co"\
,"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"} # Mostly you need to pass the headers . Default headers don't work always. So be careful here
r1=s.get(main_url,headers=headers) #authenticity token is extracted from this page using the regex i mentioned below. This is required when you login to the page
token=re.search("""<input name="authenticity_token" type="hidden" value="(.*?)"[^>]*?>""",r1.text,re.S|re.I)
print token.group(1)
headers["Referer"]="https://angel.co/login?utm_source=top_nav_home"
headers["Upgrade-Insecure-Requests"]="1"
payload={"login_only":"true","user[email]":"youremail","user[password]":"yourpassword","authenticity_token":token.group(1),"utf8":"%25E2%259C%2593"} # the value of utf8 gets urlencoded once you send the request.
r2=s.post("https://angel.co/users/login",headers=headers,data=payload,cookies=r1.cookies)
print r2.status_code