sub :子

process:进程

用法:

import subprocess
while True:
    cmd_str = inport('请输入终端命令:')
    obj = subprocrss.Popen(
        cmd_str, shell = True,
        stdout = subprocess.PIPE,
        stderr = subprocess.PIPE
    )
    success = obj.stdout.read().decode('gbk')
    if success:
        print(success, '正确结果')
    error = obj.stderr.read().decode('gbk')
    if error:
        print(error, '错误结果')
View Code

re 模块

用法:

"""
^ : 代表'开头'
$ : 代表'结束'
| : 代表'或'
{} : 需要获取的值,用于限定数量
[] : 分组限制取值范围
import re 
while True:
    phone_number = input('请输入手机号码:').strip()
    if re.match('^(13|14|15|19)[0-9]{9}$', phone_number):
        print('合法')
    else:
        print('不合法')
字符组:
    -[0-9] 可以匹配到一个0-9的字符
    -[9-0] 报错,必须从小到大
    -[a-z] 从小写的a-z
    -[A-Z] 从大写 A-Z
    -[z-A] 错误,只能从小到大,根据ASCII 表来匹配大小
    -[A-z] 从大写到A写到小写z 
    
    注意:顺序必须要按照ASCII码数值的顺序编写
    
import re 
res = re.match('[A-Z a-z 0-9]{8}', 'Tank9527')
print(res)
print(res.group())

re模块中三种比较重要方法:
    - findall(): ---> []
    可以匹配所有的字符,拿到一个返回值,返回结果是一个列表
    
    - search (): ---->obj ---> obj.group()
    在匹配一个字符成功后,拿到结果后结束,不往后匹配
    
    - match() ---> obj  ---> obj.group()
    从匹配字符的开头匹配,若开头不是想要的内容,则返回None
    
import re 
str = 'sean tank json'
res = re.findall('[z-a]{4}', str)
print(res)

import re 
str = 'sean tamk json'
res =re.search('[z-a]{4}', str)
print(res.group())

import re 
str = 'sean tank json'
res = re.match('[z-a]{4}', str)
print(res.group())
"""
View Code

爬虫爬取信息:

爬虫四部原则:

    1、发送请求    request

2、获取响应数据:对方机器直接返回的

3、解析并提取想要的数据: re

4、保存提取后的数据:with open()

爬虫三部曲:

1、发送请求

2、解析数据

3、保存数据

#1、发送请求
def get_page(url):
    response = requests.get(url)
    
    return response
#2、解析数据
def parser_page(text):
    #re.findall('正则表达式', '过滤的文本')
    res_list = re.findall(
    )
    for movie_tuple in res_list:
        #print(movie_tuple)
        yield movie_tuple 
        
#3、保存数据 
def save_date(res_list_)
    with open('xx')
View Code

 

 

posted on 2019-11-27 16:18    阅读(96)  评论(0编辑  收藏  举报