文章标题

#! /usr/bin/env python
#coding=utf-8
import urllib2,urllib,cookielib,requests
from bs4 import BeautifulSoup

class form():
    def __init__(self,username,password):
        self.username = username
        self.password = password
        self.cj=cookielib.CookieJar()
        self.handler=urllib2.HTTPCookieProcessor(self.cj)
        self.opener=urllib2.build_opener(self.handler)
        urllib2.install_opener(self.opener)

    def get_cookie(self):
        self.firstCookie = "输入访问链接"
        req=urllib2.urlopen(self.firstCookie)
        cookieDick={}
        for index,cookie in enumerate(self.cj):
            cookieDick[cookie.name] = cookie.value
        self.csrf_test_name=cookieDick.get("csrf_cookie_name")

    def access(self):
        self.host="输入host地址"
        self.loginUrl=self.host+"输入访问路径"
        self.headers={"User-agent": "UA参数",
                      "host": self.host}
        self.data={"username": self.username, "password": self.password, 'foward': 'null', 'csrf_test_name': self.csrf_test_name}
        dataEncode=urllib.urlencode(self.data)
        self.reqLogin=urllib2.Request(self.loginUrl,data=dataEncode,headers=self.headers)
        self.resLogin=urllib2.urlopen(self.reqLogin)
        self.page=self.resLogin.read()
        print self.page
class login(form):
    def go(self):
        self.get_cookie()
        self.access()

if __name__ == '__main__':
    test=login("username","password")
    test.go()

你可能感兴趣的:(python,python-爬虫)