Python代码求解释 求大神解释一下这段代码 详细点 最好每句加上注释 谢了

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python代码求解释 求大神解释一下这段代码 详细点 最好每句加上注释 谢了相关的知识,希望对你有一定的参考价值。

import SimpleHTTPServer
import SocketServer
import logging
import cgi

import urllib
import urllib2
import cookielib
def grade(uname,password):
cj=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
req=urllib2.Request("http://202.115.139.16/login.php",urllib.urlencode("uname":uname,"upwd":password))
req.add_header("Referer", "http://202.115.139.16/login.php")
d=opener.open(req)

f=opener.open("http://202.115.139.16/sys_xj_xscj/sys_xj_xscj.php")

result=''
lines = f.readlines()
list1 = lines[551:682]
list2 = lines[1:21]
for line in list2:
result+=line
for line in list1:
result+=line
result+="</table>"
#result= f.read()

fexit=opener.open("http://202.115.139.16//closesys/closesys.php")
return result

PORT = 13000

class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):

def do_GET(self):
#logging.error(self.headers)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

def do_POST(self):
#logging.error(self.headers)
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ='REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
)
uname=form.list[0]
upw=form.list[1]
uname=str(uname)[26:-2]
upw=str(upw)[24:-2]
self.send_response(200)
self.send_header("Content-type","text/html;charset=gb2312")
self.end_headers()
self.wfile.write(grade(uname,upw))

Handler = ServerHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "Service port:",PORT
httpd.serve_forever()
登陆后抓取某一学期的成绩

首先引用py中网络的相关组件
def grade(uname,password):从外部获取用户名和密码,带入opener中,按网站的接收格式向地址发送信息,等待网站返回结果:成功登录或者失败。
成功后打开成绩页面,后面就是依照成绩的格式读取相应字段(科目,成绩)写入result中
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):这是防止由于是PY发出的请求而非浏览器请教被网站拒绝而设置的伪装,同时测试该网页接口。
最后打印result。追问

能详细解释下里面的参数和方法各自的作用么

参考技术A 建立cookie,post帐号密码后,通过这个cookie去访问相关的链接。就这么简单

求python大神解释下这段代码,没接触过python不会啊

def __init__(self,
n_estimators=10,
criterion="gini",
max_depth=None,
min_samples_split=2,
min_samples_leaf=1,
min_weight_fraction_leaf=0.,
max_features="auto",
max_leaf_nodes=None,
bootstrap=True,
oob_score=False,
n_jobs=1,
random_state=None,
verbose=0,
warm_start=False,
class_weight=None):
super(RandomForestClassifier, self).__init__(
base_estimator=DecisionTreeClassifier(),
n_estimators=n_estimators,
estimator_params=("criterion", "max_depth", "min_samples_split",
"min_samples_leaf", "min_weight_fraction_leaf",
"max_features", "max_leaf_nodes",
"random_state"),
bootstrap=bootstrap,
oob_score=oob_score,
n_jobs=n_jobs,
random_state=random_state,
verbose=verbose,
warm_start=warm_start,
class_weight=class_weight)

self.criterion = criterion
self.max_depth = max_depth
self.min_samples_split = min_samples_split
self.min_samples_leaf = min_samples_leaf
self.min_weight_fraction_leaf = min_weight_fraction_leaf
self.max_features = max_features
self.max_leaf_nodes = max_leaf_nodes

这就是一段构造函数。
(self,
n_estimators=10,
criterion="gini",
max_depth=None,
min_samples_split=2,
min_samples_leaf=1,
min_weight_fraction_leaf=0.,
max_features="auto",
max_leaf_nodes=None,
bootstrap=True,
oob_score=False,
n_jobs=1,
random_state=None,
verbose=0,
warm_start=False,
class_weight=None):
这是构造函数的参数,有默认值。

super(RandomForestClassifier, self).__init__(
base_estimator=DecisionTreeClassifier(),
n_estimators=n_estimators,
estimator_params=("criterion", "max_depth", "min_samples_split",
"min_samples_leaf", "min_weight_fraction_leaf",
"max_features", "max_leaf_nodes",
"random_state"),
bootstrap=bootstrap,
oob_score=oob_score,
n_jobs=n_jobs,
random_state=random_state,
verbose=verbose,
warm_start=warm_start,
class_weight=class_weight)

supper会调用基类构造函数,你可以认为这一串就是基类构造函数的参数。

self.criterion = criterion
self.max_depth = max_depth
self.min_samples_split = min_samples_split
self.min_samples_leaf = min_samples_leaf
self.min_weight_fraction_leaf = min_weight_fraction_leaf
self.max_features = max_features
self.max_leaf_nodes = max_leaf_nodes

这一串就是属性赋值。追问

你好,十分感谢你的回答,base_estimator=DecisionTreeClassifier(),
这一个代码是什么意思呢请问

参考技术A import re
reader = open('test.txt','r')
for txt in reader.readlines():
p = r'([a-z]+|[A-z]+)'
#print re.findall(p,txt)
pattern =  re.compile(p)
match = pattern.match(txt)
if match:
print match.groups()

以上是关于Python代码求解释 求大神解释一下这段代码 详细点 最好每句加上注释 谢了的主要内容,如果未能解决你的问题,请参考以下文章

一段Delphi代码,求解释

JavaScript 求大神解释下这段防抖函数代码,最好详细越好

css求大神给加一下注释解释,谢谢~谢谢

求解释python调用api的方法

SQL2088 安装完成 但失败 找不到数据库引擎启动句柄怎麽回事 求大神 求解释 求解决

python3 使用迭代查找一个list中最小和最大值 这段代码看不懂,希望能有大神解释一下,谢谢了