第三章 模块

1. 列举常用的模块。

 math random re os sys requests time datetime calendar logging functools  json pickle struct socket  hashlib uuid django flask collections ...

2. 如何安装第三方模块?

  1.在命令行执行pip install 模块名  2.在pycharm的File---Settings--Project Interpreter 中新增  3.源码安装

3. re 的 match 和 search 区别?

  match是从字符串的头开始查找;search是从字符串中查找,找到及返回

4. 什么是正则的贪婪匹配?或 正则匹配中的贪婪模式与非贪婪模式的区别?

  贪婪匹配就是匹配符合规则最大长度,尽可能匹配;非贪婪模式在量词后加?匹配到符合规则的一个就结束

5. 如何生成一个随机数?

  使用random模块

6. 如何使用 python 删除一个文件?

  

import os
import shutil
os.remove(path) # 删除文件
os.removedirs(path) # 删除空文件夹
shutil.rmtree(path) # 删除文件夹,可以为空也可以不为空

7. logging 模块的作用?以及应用场景?

  logging模块定义的函数和类为应用程序和库的开发实现了一个灵活的事件日志系统。

  记录日志

  

8. json 序列化时,可以处理的数据类型有哪些?如何定制支持 datetime 类型?

1.可以处理的数据类型是 string、int、list、tuple、dict、bool、null

  • 2.通过自定义时间序列化转换器
#通过自定义时间序列化转换器
import
json from json import JSONEncoder from datetime import datetime class ComplexEncoder(JSONEncoder): def default(self, obj): if isinstance(obj, datetime): return obj.strftime(‘%Y-%m-%d %H:%M:%S‘) else: return super(ComplexEncoder,self).default(obj) d = { ‘name‘:‘alex‘,‘data‘:datetime.now()} print(json.dumps(d,cls=ComplexEncoder)) # {"name": "alex", "data": "2018-05-18 19:52:05"}

9. json 序列化时,默认遇到中文会转换成 unicode,如果想要保留中文怎么办?

  设置参数ensure_ascii=False  

import json
print(json.dumps("你好",ensure_ascii=False))

10. 写代码实现查看一个目录下的所有文件。  

import os
print(os.listdir(path=r'C:\Users\Administrator\Desktop\tk'))

11. 用 Python 匹配 HTML tag 的时候,<.><.?>有什么区别?

  <.>表示必须要匹配一个<和>之间的任意内容

  <.?>表示匹配零个或一个<和>之间的任意内容

12. 如何判断一个邮箱合法  

import re
pp=re.compile('[a-zA-Z0-9_-]+@[0-9A-Za-z]+(\.[0-9a-zA-Z]+)+')
if pp.match('1403179190@qq.com'):
    print('ok')

13. 请写出以字母或下划线开始, 以数字结束的正则表达式

  ‘^[a-zA-Z_].*[0-9]$’

14. 下面那些是 Python 开发网络应用的框架

1. Django

2. Yii

3. Struts

4. Rails 5. Uliweb

6. CodeIgniter

7. gevent

8. Flask

9. web2py

10. Pylons

11. Tornado

12. Twisted

13. TurboGears

15. 写 Python 爬虫都用到了那些模块, 分别是做什么用的?

  

  • 模块
    • request,发起请求
    • pyquery,解析html数据
    • beautifulsoup,解析html数据
    • lxml  from lxml.html.clean import etree   xpath解析数据
    • aiohttp,异步发送请求
  • 框架
    • pyspider,web界面的爬虫框架
    • scrapy,爬虫框架
    • selenium,模拟浏览器的爬虫框架

16. sys.path.append("/root/mods")的作用?

  sys.path.append('xxx')的作用

  • 添加搜索路径:将root/moods/目录添加到自定义模块加载的路径中

17. 列举出 Python 中比较熟知的爬虫框架

 Scrapy  selenium  Crawley  Portia  newpaper

18. 输入某年某月某日, 判断这是这一年的第几天?(可以用 Python 的内置模块)  

import time
t='2019-09-05'
struct_time=time.strptime(t,'%Y-%m-%d')
print(struct_time.tm_yday)

19. 使用过 Python 那些第三方组件?

  

Django
pymysql
SQLalchemy
bs4
pyecharts
PIL
gevent
jieba
selenium


posted @ 2019-09-05 00:08  笑得好美  阅读(234)  评论(0编辑  收藏  举报