Python_模塊

本節大綱:html

  1.模塊介紹node

  2.time&datetime模塊python

  3.randomgit

  4.osgithub

  5.sysweb

  6.json&picle算法

  7.hashlibshell

  8.XMLapache

  9.requestsjson

  10.configparser

  11.logging

 

模塊介紹

  Python Module(模塊),就是一個保存了Python代碼的文件。模塊能定義函數,類和變量。模塊裏也能包含可執行的代碼。

  文件名就是模塊名加上後綴.py,在模塊內部,模塊名存儲在全局變量__name__中,是一個string,能夠直接在module中經過__name__應用到module name

模塊分爲三種:

  · 自定義模塊

  · 內置標準模塊(又稱標準庫)

  · 開源模塊

 

導入模塊:

  · import:使客戶端(導入者)以一個總體獲取一個模塊

  · from:允許客戶端從一個模塊文件中獲取特定的變量名

  · reload:在不終止Python程序的狀況下,提供了一個從新載入模塊文件代碼的方法

1 import module
2 from module.xx.xx import xx
3 from module.xx.xx import xx as rename 
4 from module.xx.xx import *

模塊路徑:

 1 #獲取路徑
 2 import sys
 3 for i in sys.path:
 4     print(i)
 5 #輸出結果:
 6 S:\Myproject
 7 S:\Python 3.5.1\python35.zip
 8 S:\Python 3.5.1\DLLs
 9 S:\Python 3.5.1\lib                  #存放標準庫
10 S:\Python 3.5.1
11 S:\Python 3.5.1\lib\site-packages    #存放第三方庫,擴充庫
12  
13 #添加路徑
14 import sys
15 import os
16 pre_path = os.path.abspath('../')
17 sys.path.append(pre_path)

開源模塊:

自定義模塊和開源模塊的使用參http://www.cnblogs.com/wupeiqi/articles/4963027.html

#time&datetime模塊

時間相關的操做,時間有三種表示方式:

  · 時間戳        1970年1月1日以後的秒,即:time.time()

  · 格式化的字符串       2014-11-11 11:11,即time.strftime('%Y-%m-%d)

  · 結構化時間          元組包含了:年、日、星期等...time.struct_time,即time.localtime()

 1 import time
 2 print(time.time())                  #返回當前系統時間戳(1970年1月1日0時0分0秒開始)
 3 print(time.ctime())                 #輸出Tue May 17 16:07:11 2016,當前系統時間
 4 print(time.ctime(time.time() - 86400))        #將時間戳轉換爲字符串格式
 5 print(time.gmtime(time.time() - 86400))      #將時間戳轉換爲struct_time格式
 6 print(time.localtime(time.time() - 86400))     #將時間戳轉換爲struct_time格式,返回本地時間
 7 print(time.mktime(time.localtime()))         #與time.localtime()功能相反,將struct_time格式轉回成時間戳格式
 8 #time.sleep(5)                    #sleep停頓
 9 print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))  #將struct_time格式轉成指定的字符串格式
10 print(time.strptime("2016-05-17","%Y-%m-%d"))   #將字符串格式轉換成struct_time格式
11  
12  
13 print("----------------------------------------------------------------")
14 import datetime
15 print(datetime.date.today())             #輸出格式 2016-05-17
16 print(datetime.date.fromtimestamp(time.time() - 86400)) #2016-05-16 將時間戳轉成日期格式
17 current_time = datetime.datetime.now()
18 print(current_time)                 #輸出2016-05-17 16:18:28.737561
19 print(current_time.timetuple())          #返回struct_time格式
20 print(current_time.replace(2008,8,8))         #輸出2008-08-08 16:21:34.798203,返回當前時間,但指定的值將被替換
21  
22 str_to_date = datetime.datetime.strptime("28/7/08 11:20","%d/%m/%y %H:%M")  #將字符串轉換成日期格式
23 new_date = datetime.datetime.now() + datetime.timedelta(days=10)         #比如今加10天
24 new_date = datetime.datetime.now() + datetime.timedelta(days=-10)       #比如今減10天
25 new_date = datetime.datetime.now() + datetime.timedelta(hours=-10)       #比如今減10小時
26 new_date = datetime.datetime.now() + datetime.timedelta(seconds=120)      #比如今+120s
27 print(new_date)

random模塊

隨機數:

 1 import random
 2  
 3 print(random.random())          #用於生成一個0到1的隨機符點數: 0 <= n < 1.0
 4 print(random.randint(1,2))      #用於生成一個指定範圍內的整數
 5 print(random.randrange(1,10))   #從指定範圍內,按指定基數遞增的集合中獲取一個隨機數
 6 print(random.uniform(1,10))     #用於生成一個指定範圍內的隨機符點數
 7 print(random.choice('nick'))    #從序列中獲取一個隨機元素
 8 li = ['nick','jenny','car',]
 9 random.shuffle(li)              #用於將一個列表中的元素打亂
10 print(li)
11 li_new = random.sample(li,2)    #從指定序列中隨機獲取指定長度的片段(從li中隨機獲取2個元素,做爲一個片段返回)
12 print(li_new)

生成隨機驗證碼:

 1 ########## 隨機驗證碼 ############
 2 import random
 3 temp = ''
 4 for i in range(4):
 5     num = random.randrange(0,4)
 6     if num == 0 or num == 3:        #一半的機率
 7         rad2 = random.randrange(0,10)
 8         temp = temp + str(rad2)
 9     else:
10         rad1 = random.randrange(65,91)
11         c1 = chr(rad1)
12         temp = temp + c1
13 print(temp)

OS模塊

os模塊用於提供系統級別的操做

 1 os.getcwd()                 獲取當前工做目錄,即當前python腳本工做的目錄路徑
 2 os.chdir("dirname")         改變當前腳本工做目錄;至關於shell下cd
 3 os.curdir                   返回當前目錄: ('.')
 4 os.pardir                   獲取當前目錄的父目錄字符串名:('..')
 5 os.makedirs('dir1/dir2')    可生成多層遞歸目錄
 6 os.removedirs('dirname1')   若目錄爲空,則刪除,並遞歸到上一級目錄,如若也爲空,則刪除,依此類推
 7 os.mkdir('dirname')         生成單級目錄;至關於shell中mkdir dirname
 8 os.rmdir('dirname')         刪除單級空目錄,若目錄不爲空則沒法刪除,報錯;至關於shell中rmdir dirname
 9 os.listdir('dirname')       列出指定目錄下的全部文件和子目錄,包括隱藏文件,並以列表方式打印
10 os.remove()                 刪除一個文件
11 os.rename("oldname","new")  重命名文件/目錄
12 os.stat('path/filename')    獲取文件/目錄信息
13 os.sep                      操做系統特定的路徑分隔符,win下爲"\\",Linux下爲"/"
14 os.linesep                  當前平臺使用的行終止符,win下爲"\t\n",Linux下爲"\n"
15 os.pathsep                  用於分割文件路徑的字符串
16 os.name                     字符串指示當前使用平臺。win->'nt'; Linux->'posix'
17 os.system("bash command")   運行shell命令,直接顯示
18 os.environ                  獲取系統環境變量
19 os.path.abspath(path)       返回path規範化的絕對路徑
20 os.path.split(path)         將path分割成目錄和文件名二元組返回
21 os.path.dirname(path)       返回path的目錄。其實就是os.path.split(path)的第一個元素
22 os.path.basename(path)      返回path最後的文件名。如何path以/或\結尾,那麼就會返回空值。即os.path.split(path)的第二個元素
23 os.path.exists(path)        若是path存在,返回True;若是path不存在,返回False
24 os.path.isabs(path)         若是path是絕對路徑,返回True
25 os.path.isfile(path)        若是path是一個存在的文件,返回True。不然返回False
26 os.path.isdir(path)         若是path是一個存在的目錄,則返回True。不然返回False
27 os.path.join(path1[, path2[, ...]])  將多個路徑組合後返回,第一個絕對路徑以前的參數將被忽略
28 os.path.getatime(path)      返回path所指向的文件或者目錄的最後存取時間
29 os.path.getmtime(path)      返回path所指向的文件或者目錄的最後修改時間

sys模塊

用於提供對解釋器相關的操做

1 sys.argv           命令行參數List,第一個元素是程序自己路徑
2 sys.exit(n)        退出程序,正常退出時exit(0)
3 sys.version        獲取Python解釋程序的版本信息
4 sys.maxint         最大的Int值
5 sys.path           返回模塊的搜索路徑,初始化時使用PYTHONPATH環境變量的值
6 sys.platform       返回操做系統平臺名稱
7 sys.stdin          輸入相關
8 sys.stdout         輸出相關
9 sys.stderror       錯誤相關

Json&Picle模塊

用於序列化的兩個模塊

  · json,用於字符串和Python數據類型間進行轉換

  · pickle,用於Python特有的類型和Python的數據類型間進行轉換

Json模塊提供了四個功能:dumps、dump、loads、load

pickle模塊提供了四個功能:dumps、dump、loads、load

  dump()函數接受一個文件句柄和一個數據對象做爲參數,把數據對象以特定的格式保存到給定的文件中。當咱們使用load()函數從文件中取出已保存的對象時,pickle知道如何恢復這些對象到它們原本的格式。

  dumps()函數執行和dump()函數相同的序列化。取代接受流對象並將序列化後的數據保存到磁盤文件,這個函數簡單的返回序列化的數據。

  loads()函數執行和load()函數同樣的反序列化。取代接受一個流對象病區文件讀取序列化後的數據,它接受包含序列化後的數據的str對象,直接返回的對象。

 1 ##### json.loads 將字符串轉換爲python基本數據類型 列表字典 #####
 2 import json
 3 l = '["nick","jenny","car"]'
 4 print(l,type(l))
 5 l = json.loads(l)
 6 print(l,type(l))
 7  
 8 l = '{"k1":"nick","k2:":"jenny"}'
 9 print(l,type(l))
10 l = json.loads(l)
11 print(l,type(l))
12  
13 ##### json.dumps 將python的數據類型列表字典轉換爲字符串 ######
14 import json
15 l = ["nick","jenny","car"]
16 print(l,type(l))
17 l = json.dumps(l)
18 print(l,type(l))
19  
20 l = {"k1":"nick","k2:":"jenny"}
21 print(l,type(l))
22 l = json.dumps(l)
23 print(l,type(l))
24  
25 ##### json dump、load 文件相關 #####
26 import json
27 l = {"k1":"nick","k2:":"jenny"}
28 json.dump(l,open('db','w'))
29  
30 ret = json.load(open('db'))
31 print(ret)
32  

hashlib模塊

用於加密相關的操做,代替了md5模塊和sha模塊,主要提供md5(),sha1(),sha224(),sha256(),sha384()和sha512()算法

import hashlib
 
# ######## md5 ########
hash = hashlib.md5()
# help(hash.update)
hash.update(bytes('admin', encoding='utf-8'))
print(hash.hexdigest())
print(hash.digest())
 
 
######## sha1 ########
 
hash = hashlib.sha1()
hash.update(bytes('admin', encoding='utf-8'))
print(hash.hexdigest())
 
# ######## sha256 ########
 
hash = hashlib.sha256()
hash.update(bytes('admin', encoding='utf-8'))
print(hash.hexdigest())
 
 
# ######## sha384 ########
 
hash = hashlib.sha384()
hash.update(bytes('admin', encoding='utf-8'))
print(hash.hexdigest())
 
# ######## sha512 ########
 
hash = hashlib.sha512()
hash.update(bytes('admin', encoding='utf-8'))
print(hash.hexdigest())
 
 
##### 加鹽 ######
# ######## md5 ########
 
hash = hashlib.md5(bytes('898oaFs09f',encoding="utf-8"))
hash.update(bytes('admin',encoding="utf-8"))
print(hash.hexdigest())
 
#python內置還有一個 hmac 模塊,它內部對咱們建立 key 和 內容 進行進一步的處理而後再加密
 
import hmac
 
h = hmac.new(bytes('898oaFs09f',encoding="utf-8"))
h.update(bytes('admin',encoding="utf-8"))
print(h.hexdigest())

XML模塊

xml是實現不一樣語言或程序之間進行數據交換的協議,xml文件格式以下:

<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2023</year>
        <gdppc>141100</gdppc>
        <neighbor direction="E" name="Austria" />
        <neighbor direction="W" name="Switzerland" />
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2026</year>
        <gdppc>59900</gdppc>
        <neighbor direction="N" name="Malaysia" />
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2026</year>
        <gdppc>13600</gdppc>
        <neighbor direction="W" name="Costa Rica" />
        <neighbor direction="E" name="Colombia" />
    </country>
</data>

1.解析xml

from xml.etree import ElementTree as ET

# 打開文件,讀取XML內容
str_xml = open('xo.xml', 'r').read()

# 將字符串解析成xml特殊對象,root代指xml文件的根節點
root = ET.XML(str_xml)

利用ElementTree.XML將字符串解析成xml對象
利用ElementTree.XML將字符串解析成xml對象
from xml.etree import ElementTree as ET

# 直接解析xml文件
tree = ET.parse("xo.xml")

# 獲取xml文件的根節點
root = tree.getroot()

利用ElementTree.parse將文件直接解析成xml對象
利用ElementTree.parse將文件直接解析成xml對象

2.操做xml

xml格式類型是節點嵌套節點,對於每個節點均有如下功能,以便對當前節點進行操做:

class Element:
    """An XML element.

    This class is the reference implementation of the Element interface.

    An element's length is its number of subelements.  That means if you
    want to check if an element is truly empty, you should check BOTH
    its length AND its text attribute.

    The element tag, attribute names, and attribute values can be either
    bytes or strings.

    *tag* is the element name.  *attrib* is an optional dictionary containing
    element attributes. *extra* are additional element attributes given as
    keyword arguments.

    Example form:
        <tag attrib>text<child/>...</tag>tail

    """

    當前節點的標籤名
    tag = None
    """The element's name."""

    當前節點的屬性

    attrib = None
    """Dictionary of the element's attributes."""

    當前節點的內容
    text = None
    """
    Text before first subelement. This is either a string or the value None.
    Note that if there is no text, this attribute may be either
    None or the empty string, depending on the parser.

    """

    tail = None
    """
    Text after this element's end tag, but before the next sibling element's
    start tag.  This is either a string or the value None.  Note that if there
    was no text, this attribute may be either None or an empty string,
    depending on the parser.

    """

    def __init__(self, tag, attrib={}, **extra):
        if not isinstance(attrib, dict):
            raise TypeError("attrib must be dict, not %s" % (
                attrib.__class__.__name__,))
        attrib = attrib.copy()
        attrib.update(extra)
        self.tag = tag
        self.attrib = attrib
        self._children = []

    def __repr__(self):
        return "<%s %r at %#x>" % (self.__class__.__name__, self.tag, id(self))

    def makeelement(self, tag, attrib):
        建立一個新節點
        """Create a new element with the same type.

        *tag* is a string containing the element name.
        *attrib* is a dictionary containing the element attributes.

        Do not call this method, use the SubElement factory function instead.

        """
        return self.__class__(tag, attrib)

    def copy(self):
        """Return copy of current element.

        This creates a shallow copy. Subelements will be shared with the
        original tree.

        """
        elem = self.makeelement(self.tag, self.attrib)
        elem.text = self.text
        elem.tail = self.tail
        elem[:] = self
        return elem

    def __len__(self):
        return len(self._children)

    def __bool__(self):
        warnings.warn(
            "The behavior of this method will change in future versions.  "
            "Use specific 'len(elem)' or 'elem is not None' test instead.",
            FutureWarning, stacklevel=2
            )
        return len(self._children) != 0 # emulate old behaviour, for now

    def __getitem__(self, index):
        return self._children[index]

    def __setitem__(self, index, element):
        # if isinstance(index, slice):
        #     for elt in element:
        #         assert iselement(elt)
        # else:
        #     assert iselement(element)
        self._children[index] = element

    def __delitem__(self, index):
        del self._children[index]

    def append(self, subelement):
        爲當前節點追加一個子節點
        """Add *subelement* to the end of this element.

        The new element will appear in document order after the last existing
        subelement (or directly after the text, if it's the first subelement),
        but before the end tag for this element.

        """
        self._assert_is_element(subelement)
        self._children.append(subelement)

    def extend(self, elements):
        爲當前節點擴展 n 個子節點
        """Append subelements from a sequence.

        *elements* is a sequence with zero or more elements.

        """
        for element in elements:
            self._assert_is_element(element)
        self._children.extend(elements)

    def insert(self, index, subelement):
        在當前節點的子節點中插入某個節點,即:爲當前節點建立子節點,而後插入指定位置
        """Insert *subelement* at position *index*."""
        self._assert_is_element(subelement)
        self._children.insert(index, subelement)

    def _assert_is_element(self, e):
        # Need to refer to the actual Python implementation, not the
        # shadowing C implementation.
        if not isinstance(e, _Element_Py):
            raise TypeError('expected an Element, not %s' % type(e).__name__)

    def remove(self, subelement):
        在當前節點在子節點中刪除某個節點
        """Remove matching subelement.

        Unlike the find methods, this method compares elements based on
        identity, NOT ON tag value or contents.  To remove subelements by
        other means, the easiest way is to use a list comprehension to
        select what elements to keep, and then use slice assignment to update
        the parent element.

        ValueError is raised if a matching element could not be found.

        """
        # assert iselement(element)
        self._children.remove(subelement)

    def getchildren(self):
        獲取全部的子節點(廢棄)
        """(Deprecated) Return all subelements.

        Elements are returned in document order.

        """
        warnings.warn(
            "This method will be removed in future versions.  "
            "Use 'list(elem)' or iteration over elem instead.",
            DeprecationWarning, stacklevel=2
            )
        return self._children

    def find(self, path, namespaces=None):
        獲取第一個尋找到的子節點
        """Find first matching element by tag name or path.

        *path* is a string having either an element tag or an XPath,
        *namespaces* is an optional mapping from namespace prefix to full name.

        Return the first matching element, or None if no element was found.

        """
        return ElementPath.find(self, path, namespaces)

    def findtext(self, path, default=None, namespaces=None):
        獲取第一個尋找到的子節點的內容
        """Find text for first matching element by tag name or path.

        *path* is a string having either an element tag or an XPath,
        *default* is the value to return if the element was not found,
        *namespaces* is an optional mapping from namespace prefix to full name.

        Return text content of first matching element, or default value if
        none was found.  Note that if an element is found having no text
        content, the empty string is returned.

        """
        return ElementPath.findtext(self, path, default, namespaces)

    def findall(self, path, namespaces=None):
        獲取全部的子節點
        """Find all matching subelements by tag name or path.

        *path* is a string having either an element tag or an XPath,
        *namespaces* is an optional mapping from namespace prefix to full name.

        Returns list containing all matching elements in document order.

        """
        return ElementPath.findall(self, path, namespaces)

    def iterfind(self, path, namespaces=None):
        獲取全部指定的節點,並建立一個迭代器(能夠被for循環)
        """Find all matching subelements by tag name or path.

        *path* is a string having either an element tag or an XPath,
        *namespaces* is an optional mapping from namespace prefix to full name.

        Return an iterable yielding all matching elements in document order.

        """
        return ElementPath.iterfind(self, path, namespaces)

    def clear(self):
        清空節點
        """Reset element.

        This function removes all subelements, clears all attributes, and sets
        the text and tail attributes to None.

        """
        self.attrib.clear()
        self._children = []
        self.text = self.tail = None

    def get(self, key, default=None):
        獲取當前節點的屬性值
        """Get element attribute.

        Equivalent to attrib.get, but some implementations may handle this a
        bit more efficiently.  *key* is what attribute to look for, and
        *default* is what to return if the attribute was not found.

        Returns a string containing the attribute value, or the default if
        attribute was not found.

        """
        return self.attrib.get(key, default)

    def set(self, key, value):
        爲當前節點設置屬性值
        """Set element attribute.

        Equivalent to attrib[key] = value, but some implementations may handle
        this a bit more efficiently.  *key* is what attribute to set, and
        *value* is the attribute value to set it to.

        """
        self.attrib[key] = value

    def keys(self):
        獲取當前節點的全部屬性的 key

        """Get list of attribute names.

        Names are returned in an arbitrary order, just like an ordinary
        Python dict.  Equivalent to attrib.keys()

        """
        return self.attrib.keys()

    def items(self):
        獲取當前節點的全部屬性值,每一個屬性都是一個鍵值對
        """Get element attributes as a sequence.

        The attributes are returned in arbitrary order.  Equivalent to
        attrib.items().

        Return a list of (name, value) tuples.

        """
        return self.attrib.items()

    def iter(self, tag=None):
        在當前節點的子孫中根據節點名稱尋找全部指定的節點,並返回一個迭代器(能夠被for循環)。
        """Create tree iterator.

        The iterator loops over the element and all subelements in document
        order, returning all elements with a matching tag.

        If the tree structure is modified during iteration, new or removed
        elements may or may not be included.  To get a stable set, use the
        list() function on the iterator, and loop over the resulting list.

        *tag* is what tags to look for (default is to return all elements)

        Return an iterator containing all the matching elements.

        """
        if tag == "*":
            tag = None
        if tag is None or self.tag == tag:
            yield self
        for e in self._children:
            yield from e.iter(tag)

    # compatibility
    def getiterator(self, tag=None):
        # Change for a DeprecationWarning in 1.4
        warnings.warn(
            "This method will be removed in future versions.  "
            "Use 'elem.iter()' or 'list(elem.iter())' instead.",
            PendingDeprecationWarning, stacklevel=2
        )
        return list(self.iter(tag))

    def itertext(self):
        在當前節點的子孫中根據節點名稱尋找全部指定的節點的內容,並返回一個迭代器(能夠被for循環)。
        """Create text iterator.

        The iterator loops over the element and all subelements in document
        order, returning all inner text.

        """
        tag = self.tag
        if not isinstance(tag, str) and tag is not None:
            return
        if self.text:
            yield self.text
        for e in self:
            yield from e.itertext()
            if e.tail:
                yield e.tail

源碼
xml官方源碼

因爲每一個節點都具備以上的方法,而且在上一步驟中解析時均獲得了root(xml文件的根節點),因此能夠利用以上方法進行操做xml文件

a.遍歷xml文檔的全部內容

from xml.etree import ElementTree as ET

############ 解析方式一 ############
"""
# 打開文件,讀取XML內容
str_xml = open('xo.xml', 'r').read()

# 將字符串解析成xml特殊對象,root代指xml文件的根節點
root = ET.XML(str_xml)
"""
############ 解析方式二 ############

# 直接解析xml文件
tree = ET.parse("xo.xml")

# 獲取xml文件的根節點
root = tree.getroot()


### 操做

# 頂層標籤
print(root.tag)


# 遍歷XML文檔的第二層
for child in root:
    # 第二層節點的標籤名稱和標籤屬性
    print(child.tag, child.attrib)
    # 遍歷XML文檔的第三層
    for i in child:
        # 第二層節點的標籤名稱和內容
        print(i.tag,i.text)
View Code

b.遍歷xml中指定的節點

from xml.etree import ElementTree as ET

############ 解析方式一 ############
"""
# 打開文件,讀取XML內容
str_xml = open('xo.xml', 'r').read()

# 將字符串解析成xml特殊對象,root代指xml文件的根節點
root = ET.XML(str_xml)
"""
############ 解析方式二 ############

# 直接解析xml文件
tree = ET.parse("xo.xml")

# 獲取xml文件的根節點
root = tree.getroot()


### 操做

# 頂層標籤
print(root.tag)


# 遍歷XML中全部的year節點
for node in root.iter('year'):
    # 節點的標籤名稱和內容
    print(node.tag, node.text)
View Code

c.修改節點內容

因爲修改節點時,均是在內存中進行,其不會影響文件中的內容。因此,若是想要修改,則須要從新將內存中的內容寫到新文件

from xml.etree import ElementTree as ET

############ 解析方式一 ############

# 打開文件,讀取XML內容
str_xml = open('xo.xml', 'r').read()

# 將字符串解析成xml特殊對象,root代指xml文件的根節點
root = ET.XML(str_xml)

############ 操做 ############

# 頂層標籤
print(root.tag)

# 循環全部的year節點
for node in root.iter('year'):
    # 將year節點中的內容自增一
    new_year = int(node.text) + 1
    node.text = str(new_year)

    # 設置屬性
    node.set('name', 'alex')
    node.set('age', '18')
    # 刪除屬性
    del node.attrib['name']


############ 保存文件 ############
tree = ET.ElementTree(root)
tree.write("newnew.xml", encoding='utf-8')

解析字符串方式,修改,保存
解析字符串方式,修改,保存
from xml.etree import ElementTree as ET

############ 解析方式二 ############

# 直接解析xml文件
tree = ET.parse("xo.xml")

# 獲取xml文件的根節點
root = tree.getroot()

############ 操做 ############

# 頂層標籤
print(root.tag)

# 循環全部的year節點
for node in root.iter('year'):
    # 將year節點中的內容自增一
    new_year = int(node.text) + 1
    node.text = str(new_year)

    # 設置屬性
    node.set('name', 'alex')
    node.set('age', '18')
    # 刪除屬性
    del node.attrib['name']


############ 保存文件 ############
tree.write("newnew.xml", encoding='utf-8')

解析文件方式,修改,保存
解析文件方式,修改,保存

d.刪除節點

from xml.etree import ElementTree as ET

############ 解析字符串方式打開 ############

# 打開文件,讀取XML內容
str_xml = open('xo.xml', 'r').read()

# 將字符串解析成xml特殊對象,root代指xml文件的根節點
root = ET.XML(str_xml)

############ 操做 ############

# 頂層標籤
print(root.tag)

# 遍歷data下的全部country節點
for country in root.findall('country'):
    # 獲取每個country節點下rank節點的內容
    rank = int(country.find('rank').text)

    if rank > 50:
        # 刪除指定country節點
        root.remove(country)

############ 保存文件 ############
tree = ET.ElementTree(root)
tree.write("newnew.xml", encoding='utf-8')

解析字符串方式打開,刪除,保存
解析字符串方式打開,刪除,保存
from xml.etree import ElementTree as ET

############ 解析文件方式 ############

# 直接解析xml文件
tree = ET.parse("xo.xml")

# 獲取xml文件的根節點
root = tree.getroot()

############ 操做 ############

# 頂層標籤
print(root.tag)

# 遍歷data下的全部country節點
for country in root.findall('country'):
    # 獲取每個country節點下rank節點的內容
    rank = int(country.find('rank').text)

    if rank > 50:
        # 刪除指定country節點
        root.remove(country)

############ 保存文件 ############
tree.write("newnew.xml", encoding='utf-8')

 解析文件方式打開,刪除,保存
解析文件方式打開,刪除,保存

3.建立xml文檔

from xml.etree import ElementTree as ET


# 建立根節點
root = ET.Element("famliy")


# 建立節點大兒子
son1 = ET.Element('son', {'name': '兒1'})
# 建立小兒子
son2 = ET.Element('son', {"name": '兒2'})

# 在大兒子中建立兩個孫子
grandson1 = ET.Element('grandson', {'name': '兒11'})
grandson2 = ET.Element('grandson', {'name': '兒12'})
son1.append(grandson1)
son1.append(grandson2)


# 把兒子添加到根節點中
root.append(son1)
root.append(son1)

tree = ET.ElementTree(root)
tree.write('oooo.xml',encoding='utf-8', short_empty_elements=False)

建立方式(一)
建立方式(一)
from xml.etree import ElementTree as ET

# 建立根節點
root = ET.Element("famliy")


# 建立大兒子
# son1 = ET.Element('son', {'name': '兒1'})
son1 = root.makeelement('son', {'name': '兒1'})
# 建立小兒子
# son2 = ET.Element('son', {"name": '兒2'})
son2 = root.makeelement('son', {"name": '兒2'})

# 在大兒子中建立兩個孫子
# grandson1 = ET.Element('grandson', {'name': '兒11'})
grandson1 = son1.makeelement('grandson', {'name': '兒11'})
# grandson2 = ET.Element('grandson', {'name': '兒12'})
grandson2 = son1.makeelement('grandson', {'name': '兒12'})

son1.append(grandson1)
son1.append(grandson2)


# 把兒子添加到根節點中
root.append(son1)
root.append(son1)

tree = ET.ElementTree(root)
tree.write('oooo.xml',encoding='utf-8', short_empty_elements=False)

建立方式(二)
建立方式(二)
from xml.etree import ElementTree as ET


# 建立根節點
root = ET.Element("famliy")


# 建立節點大兒子
son1 = ET.SubElement(root, "son", attrib={'name': '兒1'})
# 建立小兒子
son2 = ET.SubElement(root, "son", attrib={"name": "兒2"})

# 在大兒子中建立一個孫子
grandson1 = ET.SubElement(son1, "age", attrib={'name': '兒11'})
grandson1.text = '孫子'


et = ET.ElementTree(root)  #生成文檔對象
et.write("test.xml", encoding="utf-8", xml_declaration=True, short_empty_elements=False)

 建立方式(三)
建立方式(三)

因爲原生保存xml是默認無縮進,若是想要設置縮進的話,須要修改保存方式:

from xml.etree import ElementTree as ET
from xml.dom import minidom


def prettify(elem):
    """將節點轉換成字符串,並添加縮進。
    """
    rough_string = ET.tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="\t")

# 建立根節點
root = ET.Element("famliy")


# 建立大兒子
# son1 = ET.Element('son', {'name': '兒1'})
son1 = root.makeelement('son', {'name': '兒1'})
# 建立小兒子
# son2 = ET.Element('son', {"name": '兒2'})
son2 = root.makeelement('son', {"name": '兒2'})

# 在大兒子中建立兩個孫子
# grandson1 = ET.Element('grandson', {'name': '兒11'})
grandson1 = son1.makeelement('grandson', {'name': '兒11'})
# grandson2 = ET.Element('grandson', {'name': '兒12'})
grandson2 = son1.makeelement('grandson', {'name': '兒12'})

son1.append(grandson1)
son1.append(grandson2)


# 把兒子添加到根節點中
root.append(son1)
root.append(son1)


raw_str = prettify(root)

f = open("xxxoo.xml",'w',encoding='utf-8')
f.write(raw_str)
f.close()

寫入縮進
寫入縮進

4.命名空間

from xml.etree import ElementTree as ET

ET.register_namespace('com',"http://www.company.com") #some name

# build a tree structure
root = ET.Element("{http://www.company.com}STUFF")
body = ET.SubElement(root, "{http://www.company.com}MORE_STUFF", attrib={"{http://www.company.com}hhh": "123"})
body.text = "STUFF EVERYWHERE!"

# wrap it in an ElementTree instance, and save as XML
tree = ET.ElementTree(root)

tree.write("page.xml",
           xml_declaration=True,
           encoding='utf-8',
           method="xml")

命名空間
命名空間

requests模塊

  Python標準庫中提供了:urllib等模塊以供http請求,可是,它的API太渣了。他是爲另外一個時代、另外一個互聯網所建立的。它須要巨量的工做,甚至包括各類方法覆蓋,來完成最簡單的任務。

  requests是使用apache2 licensed許可證的基於Python開發的HTTP庫,其在Python內置模塊的基礎上進行了高度的封裝,從而使得Pythoner進行網絡請求時,變得美好了許多,使用Requests能夠垂手可得的完成瀏覽器可有的任何操做

1.安裝模塊

  pip3 install requests

2.使用模塊

# 一、無參數實例
 
import requests
 
ret = requests.get('https://github.com/timeline.json')
 
print(ret.url)
print(ret.text)
 
 
 
# 二、有參數實例
 
import requests
 
payload = {'key1': 'value1', 'key2': 'value2'}
ret = requests.get("http://httpbin.org/get", params=payload)
 
print(ret.url)
print(ret.text)

GET請求
GET請求
# 一、基本POST實例
 
import requests
 
payload = {'key1': 'value1', 'key2': 'value2'}
ret = requests.post("http://httpbin.org/post", data=payload)
 
print(ret.text)
 
 
# 二、發送請求頭和數據實例
 
import requests
import json
 
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
headers = {'content-type': 'application/json'}
 
ret = requests.post(url, data=json.dumps(payload), headers=headers)
 
print(ret.text)
print(ret.cookies)

 POST請求
POST請求
requests.get(url, params=None, **kwargs)
requests.post(url, data=None, json=None, **kwargs)
requests.put(url, data=None, **kwargs)
requests.head(url, **kwargs)
requests.delete(url, **kwargs)
requests.patch(url, data=None, **kwargs)
requests.options(url, **kwargs)
 
# 以上方法均是在此方法的基礎上構建
requests.request(method, url, **kwargs)

 其餘請求
其餘請求

更多requests模塊相關的文檔見:http://cn.python-requests.org/zh_CN/latest/

3.http請求和xml實例

實例:檢測QQ帳號是否在線

import urllib
import requests
from xml.etree import ElementTree as ET

# 使用內置模塊urllib發送HTTP請求,或者XML格式內容
"""
f = urllib.request.urlopen('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=630571017')
result = f.read().decode('utf-8')
"""


# 使用第三方模塊requests發送HTTP請求,或者XML格式內容
r = requests.get('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=424662508')
result = r.text

# 解析XML格式內容
node = ET.XML(result)

# 獲取內容
if node.text == "Y":
    print("在線")
else:
    print("離線")

檢測QQ帳號是否在線
檢測QQ帳號是否在線

實例:查看火車停靠信息

import urllib
import requests
from xml.etree import ElementTree as ET

# 使用內置模塊urllib發送HTTP請求,或者XML格式內容
"""
f = urllib.request.urlopen('http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=G666&UserID=')
result = f.read().decode('utf-8')
"""

# 使用第三方模塊requests發送HTTP請求,或者XML格式內容
r = requests.get('http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=G666&UserID=')
result = r.text

# 解析XML格式內容
root = ET.XML(result)
for node in root.iter('TrainDetailInfo'):
    print(node.find('TrainStation').text,node.find('StartTime').text,node.tag,node.attrib)

查看火車停靠信息
查看火車停靠信息

實例:查看天氣信息

import requests

response = requests.get("http://www.weather.com.cn/data/sk/101010100.html")
response.encoding = "utf-8"
result = response.text
print(result)
查看天氣信息

configparser模塊

configparser模塊用於處理特定格式的文件,其本質上是利用open來操做文件

#指定格式

#註釋
;註釋2

[nick]           #節點
age = 18         #
gender = ning    #
dearm = girl     #

[jenny]          #節點
age = 21         #
gender = jia     #

1.獲取全部節點

1 import configparser
2  
3 con = configparser.ConfigParser()
4 con.read("ini",encoding="utf-8")
5  
6 result = con.sections()
7 print(result)

2.獲取指定節點下全部的鍵值對

1 import configparser
2  
3 con = configparser.ConfigParser()
4 con.read("ini",encoding="utf-8")
5  
6 result = con.items("nick")
7 print(result)

3.獲取指定節點下全部的鍵

1 import configparser
2  
3 con = configparser.ConfigParser()
4 con.read("ini",encoding="utf-8")
5  
6 ret = con.options("nick")
7 print(ret)

4.獲取指定節點下指定key的值

 1 import configparser
 2  
 3 con = configparser.ConfigParser()
 4 con.read("ini",encoding="utf-8")
 5  
 6 v = con.get("nick","age")
 7 v = con.get("nick","gender")
 8 v = con.get("jenny","age")
 9 v = con.get("jenny","gender")
10 print(v)

5.檢查、刪除、添加節點

 1 #檢查、刪除、添加節點
 2 import configparser
 3  
 4 con = configparser.ConfigParser()
 5 con.read("ini",encoding="utf-8")
 6  
 7 #檢查
 8 has_sec = con.has_section("nick")
 9 print(has_sec)
10  
11 #添加節點
12 con.add_section("car")
13 con.write(open("ini","w"))
14  
15 #刪除節點
16 con.remove_section("car")
17 con.write(open("ini","w"))

6.檢查、刪除、設置指定組內的鍵值對

 1 #檢查、刪除、設置指定組內的鍵值對
 2 import configparser
 3  
 4 con = configparser.ConfigParser()
 5 con.read("ini",encoding="utf-8")
 6  
 7 #檢查
 8 hac_opt = con.has_option("nick","age")
 9 print(hac_opt)
10  
11 #刪除
12 con.remove_option("nick","dearm")
13 con.write(open("ini","w"))
14  
15 #設置
16 con.set("nick","dearm","girl")
17 con.write(open("ini","w"))

logging模塊

用於便捷記錄日誌且線程安全的模塊

1.單日誌文件

 1 import logging
 2  
 3 logging.basicConfig(filename="log.log",
 4                     format="%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s",
 5                     datefmt="%Y-%m-%d %H:%M:%S %p",
 6                     level=logging.INFO)
 7  
 8 logging.critical("critical")
 9 logging.fatal("fatal")
10 logging.error("error")
11 logging.warn("warn")
12 logging.warning("warning")
13 logging.info("info")
14 logging.debug("debug")
15 logging.log(8,"log")

日誌等級

"""
CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0
"""

只有【當前寫等級】大於【日誌等級】時,日誌文件才被記錄

2.多文件日誌

對於上述記錄日誌的功能,只能將日誌記錄在單文件中,若是想要設置多個日誌文件,logging.basicConfig將沒法完成,須要自定義文件和日誌操做對象

 1 # 定義文件
 2 file_1_1 = logging.FileHandler('l1_1.log', 'a')
 3 fmt = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s")
 4 file_1_1.setFormatter(fmt)
 5 
 6 file_1_2 = logging.FileHandler('l1_2.log', 'a')
 7 fmt = logging.Formatter()
 8 file_1_2.setFormatter(fmt)
 9 
10 # 定義日誌
11 logger1 = logging.Logger('s1', level=logging.ERROR)
12 logger1.addHandler(file_1_1)
13 logger1.addHandler(file_1_2)
14 
15 
16 # 寫日誌
17 logger1.critical('1111')
18 
19  日誌(一)
日誌(一)
 1 # 定義文件
 2 file_2_1 = logging.FileHandler('l2_1.log', 'a')
 3 fmt = logging.Formatter()
 4 file_2_1.setFormatter(fmt)
 5 
 6 # 定義日誌
 7 logger2 = logging.Logger('s2', level=logging.INFO)
 8 logger2.addHandler(file_2_1)
 9 
10 日誌(二)
日誌(二)

如上述建立的兩個日誌對象

  · 當使用logger1寫日誌時,會將相應的內容寫入l1_1.log和l1_2.log文件中

  · 當使用logger2寫日誌時,會將相應的內容寫入l2_1.log文件中

 1 import os
 2 import logging
 3 from logging.handlers import TimedRotatingFileHandler
 4 
 5     base = os.path.abspath(os.path.dirname(__file__))
 6     logfile = os.path.join(base, 'test', 'testlog')
 7     handler = TimedRotatingFileHandler(filename=logfile, when='MIDNIGHT',
 8                                                       interval=1, backupCount=365)
 9     handler.suffix = "%Y%m%d.log"
10     handler.setFormatter(logging.Formatter('%(asctime)s\t%(levelname)-8s\t%(message)s'))
11     handler.setLevel(logging.DEBUG)
12     apipartnerlogger = logging.getLogger(logfile)
13     apipartnerlogger.addHandler(handler)
14     apipartnerlogger.setLevel(logging.INFO)
15 
16 記錄日誌及按天切割實例
日誌
相關文章
相關標籤/搜索