博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python入门记
阅读量:7274 次
发布时间:2019-06-29

本文共 3327 字,大约阅读时间需要 11 分钟。

#!/usr/bin/env python3 指定执行该脚本的解释器

// 称为地板除,两个整数的除法仍然是整数

# -*- coding: utf-8 -*-  按照UTF-8编码读取源代码

函数参数

  *args是可变参数,args接收的是一个tuple;

  **kw是关键字参数,kw接收的是一个dict。

 

functools.partial(int, base=2)  偏函数

  >>>int2 = functools.partial(int, base=2)

  >>>int2('10010')

   相当于

  >>>kw = { 'base': 2 }

  >>>int('10010', **kw)

 

 

 

 

 

1 #by http://blog.csdn.net/qq_32190139/article/details/78158158 2 #湖南农大OJ http://210.43.224.19/oj/ 3  4 import requests 5 import urllib 6 import re 7 from bs4 import BeautifulSoup 8  9 loginurl = 'http://210.43.224.19/oj/login.php'  # 实际提交表单页面10 headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'} #UA 使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等11 12 13 def getcsrf():14 firsturl = 'http://210.43.224.19/oj/csrf.php' # 获取csrf请求页面15 page = urllib.request.urlopen(firsturl) # 发起get请求16 html = page.read()17 html = html.decode('utf-8') # 记得转码18 t = re.compile(r'name="csrf" value="(.+)" ') # 使用正则表达式进行匹配19 list1 = t.findall(html) #找到html中的value20 return list1[0]21 22 23 def login(datas,k):24 geturl = 'http://210.43.224.19/oj/problemset.php'+'?page='+str(k) # 需要爬取的登录之后的页面25 s = requests.session() # 构建session对象 会话对象requests.Session能够跨请求地保持某些参数,比如cookies26 Post = s.post(loginurl, data=datas, headers=headers) # post数据27 Get = s.get(geturl, cookies=Post.cookies, headers=headers)#其实这个页面不需要cookies也能进入,所以这里只是模拟登录28 return Get.content # 返回html29 30 31 def download(html):32 Soup = BeautifulSoup(html, "html.parser")33 list = Soup.findAll('tr',attrs={
'class':{
'evenrow','oddrow'},'align':''})34 href = re.compile(r'href="(.+)">')35 url = 'http://210.43.224.19/oj/'36 for i in list:37 f.write("{:<60}".format(i.text) + url + i.find('a').attrs['href']+'\n')38 39 f = open("out1.txt", "w")40 k = 141 csrf = getcsrf()42 paprm = {
'user_id': 'cz476325514', 'password': 'zitong', 'csrf': csrf} # 从登录发送的网络请求找到43 while k <= 13:44 html = login(paprm,k)45 download(html)46 k = k+147 f.close()48 #下面是每一条题目标签的构造49 #
1000
贺细平
模拟登录 爬OJ题目

 

 

nice code:

str.capitalize() 返回str的首字母大写,其他全小写

str.title()  将str的所有单词首字母大写,其他全小写

str.count(sub, start= 0,end=len(string))  字符串匹配,返回str中sub的个数

str.endswith(suffix[, start[, end]])  suffix是匹配的字符串,如以该字符串结尾,返回T

calendar.month(2016, 1)  返回该月日历(import calendar

datetime.date.isocalendar()  返回格式如(year,month,day)的元组,(2017, 15, 6)   其中date=date(2018,6,1)   https://blog.csdn.net/alvin930403/article/details/54089087

join()    list = [1, 2, 3, 4, 5]    ','.join(list) ==1,2,3,4,5

math.factorial(num)  返回num的阶乘

reversed(seq) seq -- 要转换的序列,可以是 tuple, string, list 或 range。返回一个反转的迭代器

enumerate()   将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标---[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]

isupper() 方法检测字符串中所有的字母是否都为大写。

strip([chars])  移除字符串头尾指定的字符(默认为空格)

str.encode(encoding='UTF-8',errors='strict')    errors:设置不同错误的处理方案 默认为 'strict'

pickle.dumps()  把任意对象序列化成一个bytes  .load()反序列化

json.dumps(d) 返回一个str,内容是标准的JSON   .load()反序列化

time.strftime(format[, t])以时间元组,并返回以可读字符串表示的当地时间,格式由参数format决定。t是一个struct_time对象。

转载于:https://www.cnblogs.com/tony-/p/8397368.html

你可能感兴趣的文章
汉诺塔问题的递归实现(扩展)
查看>>
隐藏php,nginx,apache的版本号以及伪装nginx,apache
查看>>
比尔盖茨在哈佛大学毕业典礼上的演讲稿(英)
查看>>
使用vim开发php,tag实现类 函数 变量跳转
查看>>
shell脚本及应用(二)
查看>>
我的友情链接
查看>>
windows下配置nginx+php环境
查看>>
Linux VFS 虚拟文件系统数据结构
查看>>
Laravel5.5 配置多模块
查看>>
mac os x 更新android sdk manager的方法
查看>>
讲下clone克隆的理解
查看>>
我讨厌注解式的Spring IOC
查看>>
大龄程序媛之歌
查看>>
php 去除数组某一个元素
查看>>
Ehcache(08)可阻塞的Cache-BlockingCache
查看>>
Spring-Boot(六) Spring Session 分布式会话管理
查看>>
iptables进行端口转发
查看>>
基于Hadoop的云盘系统客户端技术选型说明
查看>>
linux下C语言多线程编程实例--修改后
查看>>
我的友情链接
查看>>