Python requets 登录51cto家园

#-coding:utf-8 -*-

import requests
import urllib
from bs4 import BeautifulSoup

HEADERS={
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36",
    "Content-Type": "application/x-www-form-urlencoded",
}

URL="http://home.51cto.com/index"

params={
    "reback":"http://www.51cto.com/",
    "_csrf":None,
    "LoginForm[username]":"账号",
    "LoginForm[password]": "密码",
    "LoginForm[rememberMe]":0,
    "login-button":"登 录"
}

client = requests.Session()
resp = client.get(URL)
soup = BeautifulSoup(resp.content, "html.parser")
csrf = soup.find(attrs={"name":"csrf-token"})['content']
params["_csrf"] = csrf
params = urllib.urlencode(params)

resp = client.post(URL, data=params, headers=HEADERS)
#print resp.content
#soup = BeautifulSoup(resp.content, "html.parser")
#for src in soup.find_all("script"):
#    client.get(src.get("src"))
resp = client.get(URL)
print resp.content

 

posted on 2016-05-09 21:26  漫步的影子  阅读(383)  评论(2编辑  收藏  举报