Python basic knowledge of the whole network the most complete 2 (introduction module)

Use standard library 1.Python

  • Open the standard library (IDLE) : Help -> Python Docs

  • Python module exchange community : https://pypi.python.org/pypi
    module There have submitted lovers worldwide python

  • Quick overview module :

import demo
print(demo.__doc__)      #查看demo模块的相关介绍
dir(demo)                #查看demo模块内置的方法               
demo.__all__             #查看这个模块所有可以供外界调用的所有东西的列表(类名 , 方法名)。
demo.__file__            #指明该模块源代码所在的位置
help(demo)               #获取demo模块的信息,比doc的信息更详细
  • Demo Import from : The names of all __all__ inside into namespaces

That can be imported directly using the name

2, random module

module for randomly generated random
Example:

import random
secret = random.randint(1,100)     # 随机生成1~100以内的数字

3, os module

(1) .os module is a function of the file:

os module function of the file

os module function of the file

(2) .os.path function module

Instructions:

os.path.函数名

os.path function module

4.pickle module

Storage: pickling
read: unpickling

(1).dump():

pickle.dump (objects, file)

Example:

my_list = [123, 3.14, '小甲鱼', ['another list']]
pickle_file = open( 'my_list.pkl', 'wb')
pickle.dump(my_list, pickle_file)

(2).load():

pickle.load(文件名/路径)      #读取文件

5.easygui module

Import:

import  easygui  as g        #推荐用法

6.time module

(1) Time-tuple (time.struct_time):

Time tuple

7.re module (regular expressions)

(1) Method Module:

  • 1.re.search (r 'format', 'string') :
    Example: Matching IP (format):
(([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])\.){3}([01]{0,1}\d{0,1}\d|2[0-4]\d|25[0-5])
  • 2.re.findall () : matches all characters in a manner to return a list of
    cases:
rs.findall(r'[a-z]','FishC.com')  
结果:
    ['i','s','h','c','o','m']
  • 3.re.compile (r 'format') : Create Object Model

Example:

p = re.compile(r'[A-Z]')
p.findall('I Love')    
结果:['I','L']
  • 4.result.group () **: Results obtained matching
    Example:
result = re.search(r'(\w+) (\w+)','I love FishC.com!')
result.group()        结果:'love FishC'
result.group(1)       结果:'love'
result.group(2)       结果:'FishC'
  • 5.result.start(): 开始的位置(不是下标,下标从0开始)

  • 6.result.end(): 结束的位置

  • 7.result.span(): 字符串中字符位置的范围

(2).基本语法:

  • 1.\num(****数字): 如果num值为1~99,则\n(数字)表示匹配到的前面第n个子组对应的字符串

例:

(a)(b)(c)\2  à  abcb

如果num是以0开头,或者3个数字的长度,那么就匹配八进制数字所对应的ASCII码值对应的字符

  • 2.取消贪婪模式:

例: aaaaaaaaaaaaaaa
<.+> 会匹配到aaaaaaaaaaaaaaa
<.+?> 会匹配到 **–>**非贪婪模式

  • 3.[^a-z]: 匹配除了a-z之外的所有字符,^放在后面就表示匹配 ^ 本身

  • 4.[\n]: 匹配\n 相当于\转义字符

  • 5.\b: 匹配一个单词边界,单词被定义为Unidcode的字母数字或下横线字符

  • 6.\B: 匹配非单词边界,其实就是与\b相反

  • 7.编译标志:
    Compile flags

Compile flags

Compile flags

Compile flags

8.Scrapy框架

(1).安装

  • 1.安装pywin32:下载安装

  • 2.安装pip: Python\Scripts\pip.exe

  • 3.安装lxml: pip install lxml

  • 4.安装OpenSSL: pip install OpenSSL

  • 5.安装Scrapy: pip install Scrapy

注意: 安装以上步骤之前要确保有Visual c++ 相应版本的支持

  • 6.Selector是一个选择器,他有四个基本方法:

The four basic methods Selector
获取网页信息: 进入项目根目录,运行cmd

scrappy shell '网站地址'           #获取目标(会得到response)
response.body                     #得到网页源代码
response.headers                  #网页的头信息

筛选:根据response的信息用以上Selector中的函数进行筛选

response.xpath() == response.selector.xpath()

filter
例:

response.xpath('//title')                     #返回一个列表
response.xpath('//title/text()').extract()    #返回标签中的字符串的列表
response.xpath('//ul/li/a/@href').extract()   #返回所有ul标签中全部li标签中href属性的值的列表
  • 7.项目结构(tutorial为项目名):

Project structure

(2).使用

  • 1.创建项目:
scrapy startproject '项目名'

这一步会在cmd运行的文件夹下创建一个以该项目名为名的文件夹

  • 2.定义Item****容器: 在items.py中写入字段

  • 3.编写爬虫:

在spiders文件夹下创建py文件,用于编写爬虫类Spider,Spider是用户编写的用于从网站上爬取数据的类。Spider类包含用于下载的初始URL,然后是分析网页中的内容,还有提取item方法
注意: py文件里有一个name(爬虫名称)属性非常重要,是识别该爬虫的唯一标识

  • 4.获取网页信息:
test = scrappy.selector.Selector(response)
  • 5.筛选:
sites = test.xpath(//ul[@class="directory-url"]/li) .extract()
# 获取所有class为'directory-url'的ul标签里的全部li标签所包含的字符串
  • 6. Output data :
scrapy crawl dmoz –o items.json –t json

cmd run reptiles (debug) : scrapy crawl reptile name

9.Tkinter module (GUI ultimate choice)

No

Guess you like

Origin blog.csdn.net/affluent6/article/details/91533717