简单网站登录 python3

2023-01-31 04:01:59 简单 网站登录 python3

import urllib.request
#导入模块

def urlopen(url):

    
    req = urllib.request.Request(url)

    req.add_header("User-Agent","Mozilla/5.0 (windows NT 6.1; WOW64) AppleWEBKit/537.36 (Khtml, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
    #这个是加入headers
    #方式一 这个是提交的数据 就账号和密码
    #data = {}  
    #data['username']='暗夜之隐'
    #data['passWord']='**********'   这个密码要改哈
    #方式二
    data = {'username':'暗夜之隐','password':'563249423'}

    data = urllib.parse.urlencode(data,encoding='gbk').encode('gbk')
#这个urllib.parse.urlencode()  这个函数是把提交的数据变成python 可读的  如果有中文 要加encoding='gbk'或encoding='gb2312'

    html = urllib.request.urlopen(req,data)
#然后 urlopen 加上data 就可以了 
    html = html.read()

    return html




url = 'Http://www.biquge.com.tw/login.PHP?do=submit&action=login&usecookie=1&jumpurl='

html = urlopen(url)

html= html.decode('gbk')

print(html)

改进版


import urllib.request


def urlopen(url):

    
    req = urllib.request.Request(url)

    req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
    #方式一
    #data = {}  
    #data['username']='暗夜之隐'
    #data['password']='563249423'
    #方式二
    data = {'username':'暗夜之隐','password':'563249423','usecookie':'0','action':'login','submit':'(unable to decode value)'}
    
    
    

    data = urllib.parse.urlencode(data,encoding='gbk').encode('utf-8')

    print(data)
    print(url)

    html = urllib.request.urlopen(req,data)

    html = html.read()

    return html




url = 'http://www.biquge.com.tw/login.php?do=submit'

html = urlopen(url)

html= html.decode('gbk')

print(html)

相关文章