python基础学习
类和对象
- 类(Class): 用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法
class ClassName:
<statement-1>
.
.
.
<statement-N>
- 对象:通过类定义的数据结构实例。对象包括两个数据成员(类变量和实例变量)和方法
class MyClass:
"""一个简单的类实例"""
i = 12345
def f(self):
return 'hello world'
# 实例化类
x = MyClass()
# 访问类的属性和方法
print("MyClass 类的属性 i 为:", x.i)
print("MyClass 类的方法 f 输出为:", x.f())
>>>MyClass 类的属性 i 为: 12345
>>>MyClass 类的方法 f 输出为: hello world
正则表达式
re模块
re.match函数
re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。
re.match(pattern, string, flags=0)
- pattern:匹配的正则表达式
- string:要匹配的字符串
- flags:标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等
import re
print(re.match('www', 'www.runoob.com').span()) # 在起始位置匹配
print(re.match('com', 'www.runoob.com')) # 不在起始位置匹配
>>>(0, 3)
>>>None
使用group(num) 或 groups() 匹配对象函数来获取匹配表达式
- group(num=0):匹配的整个表达式的字符串,group() 可以一次输入多个组号,在这种情况下它将返回一个包含那些组所对应值的元组。
- groups():返回一个包含所有小组字符串的元组,从 1 到 所含的小组号。
import re
line = "Cats are smarter than dogs"
# .* 表示任意匹配除换行符(\n、\r)之外的任何单个或多个字符
matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I)
if matchObj:
print ("matchObj.group() : ", matchObj.group())
print ("matchObj.group(1) : ", matchObj.group(1))
print ("matchObj.group(2) : ", matchObj.group(2))
else:
print ("No match!!")
>>>matchObj.group() : Cats are smarter than dogs
>>>matchObj.group(1) : Cats
>>>matchObj.group(2) : smarter
re.search方法
re.search 扫描整个字符串并返回第一个成功的匹配。
re.search(pattern, string, flags=0)
- pattern:匹配的正则表达式
- string:要匹配的字符串
- flags:标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等
import re
print(re.search('www', 'www.runoob.com').span()) # 在起始位置匹配
print(re.search('com', 'www.runoob.com').span()) # 不在起始位置匹配
>>>(0, 3)
>>>(11, 14)
import re
line = "Cats are smarter than dogs";
searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I)
if searchObj:
print ("searchObj.group() : ", searchObj.group())
print ("searchObj.group(1) : ", searchObj.group(1))
print ("searchObj.group(2) : ", searchObj.group(2))
else:
print ("Nothing found!!")
>>>searchObj.group() : Cats are smarter than dogs
>>>searchObj.group(1) : Cats
>>>searchObj.group(2) : smarter
re.match与re.search的区别:
re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配。
import re
line = "Cats are smarter than dogs";
matchObj = re.match( r'dogs', line, re.M|re.I)
if matchObj:
print ("match --> matchObj.group() : ", matchObj.group())
else:
print ("No match!!")
matchObj = re.search( r'dogs', line, re.M|re.I)
if matchObj:
print ("search --> matchObj.group() : ", matchObj.group())
else:
print ("No match!!")
>>>No match!!
>>>search --> matchObj.group() : dogs
http请求
get请求
import requests
r = requests.get('https://book.douban.com', params={'q':'python', 'cat':'1001'})
print('状态码:', r.status_code)
print('实际请求的RUL:', r.url)
print('页面编码:', r.encoding)
print('Cookie:\n%s' % r.cookies['bid'])
print('响应头:', r.headers)
print('页面源码byte类型:\n%s' % r.content)
print('页面源码 :', r.text)
post请求
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("https://book.douban.com", data=payload)
print (r.text)
认证
url = 'https://book.douban.com'
r = requests.post(url, data={})
print (r.status_code)
print (r.headers)
print (r.reason)