欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?xpath,JsonPath,bs4的基本使用

 更新時(shí)間:2022年07月03日 17:00:55   投稿:hqx  
這篇文章主要介紹了Python?xpath,JsonPath,bs4的基本使用,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下

1.xpath

1.1 xpath使用

  • google提前安裝xpath插件,按ctrl + shift + x 出現(xiàn)小黑框
  • 安裝lxml庫 pip install lxml ‐i https://pypi.douban.com/simple
  • 導(dǎo)入lxml.etreefrom lxml import etree
  • etree.parse() 解析本地文件html_tree = etree.parse('XX.html')
  • etree.HTML() 服務(wù)器響應(yīng)文件html_tree = etree.HTML(response.read().decode('utf‐8')
  • .html_tree.xpath(xpath路徑)

1.2 xpath基本語法

1.路徑查詢

  • 查找所有子孫節(jié)點(diǎn),不考慮層級關(guān)系 
  • 找直接子節(jié)點(diǎn)

2.謂詞查詢

//div[@id] 
//div[@id="maincontent"] 

3.屬性查詢

//@class 

4.模糊查詢

//div[contains(@id, "he")] 
//div[starts‐with(@id, "he")] 

5.內(nèi)容查詢

//div/h1/text() 

6.邏輯運(yùn)算

//div[@id="head" and @class="s_down"] 
//title | //price

1.3 示例

xpath.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
</head>
<body>
    <ul>
        <li id="l1" class="class1">北京</li>
        <li id="l2" class="class2">上海</li>
        <li id="d1">廣州</li>
        <li>深圳</li>
    </ul>
</body>
</html>
from lxml import etree

# xpath解析
# 本地文件:                                          etree.parse
# 服務(wù)器相應(yīng)的數(shù)據(jù)    response.read().decode('utf-8')  etree.HTML()


tree = etree.parse('xpath.html')

# 查找url下邊的li
li_list = tree.xpath('//body/ul/li')
print(len(li_list))  # 4

# 獲取標(biāo)簽中的內(nèi)容
li_list = tree.xpath('//body/ul/li/text()')
print(li_list)  # ['北京', '上海', '廣州', '深圳']

# 獲取帶id屬性的li
li_list = tree.xpath('//ul/li[@id]')
print(len(li_list))  # 3

# 獲取id為l1的標(biāo)簽內(nèi)容
li_list = tree.xpath('//ul/li[@id="l1"]/text()')
print(li_list)  # ['北京']

# 獲取id為l1的class屬性值
c1 = tree.xpath('//ul/li[@id="l1"]/@class')
print(c1)  # ['class1']

# 獲取id中包含l的標(biāo)簽
li_list = tree.xpath('//ul/li[contains(@id, "l")]/text()')
print(li_list)  # ['北京', '上海']
# 獲取id以d開頭的標(biāo)簽
li_list = tree.xpath('//ul/li[starts-with(@id,"d")]/text()')
print(li_list)  # ['廣州']
# 獲取id為l2并且class為class2的標(biāo)簽
li_list = tree.xpath('//ul/li[@id="l2" and @class="class2"]/text()')
print(li_list)  # ['上海']
# 獲取id為l2或id為d1的標(biāo)簽
li_list = tree.xpath('//ul/li[@id="l2"]/text() | //ul/li[@id="d1"]/text()')
print(li_list)  # ['上海', '廣州']

1.4 爬取百度搜索按鈕的value

import urllib.request
from lxml import etree
url = 'http://www.baidu.com'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}
request = urllib.request.Request(url=url, headers=headers)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
tree = etree.HTML(content)
value = tree.xpath('//input[@id="su"]/@value')
print(value)

1.5 爬取站長素材的圖片

# 需求 下載的前十頁的圖片
# https://sc.chinaz.com/tupian/qinglvtupian.html   1
# https://sc.chinaz.com/tupian/qinglvtupian_page.html
import urllib.request
from lxml import etree
def create_request(page):
    if (page == 1):
        url = 'https://sc.chinaz.com/tupian/qinglvtupian.html'
    else:
        url = 'https://sc.chinaz.com/tupian/qinglvtupian_' + str(page) + '.html'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36',
    }
    request = urllib.request.Request(url=url, headers=headers)
    return request
def get_content(request):
    response = urllib.request.urlopen(request)
    content = response.read().decode('utf-8')
    return content
def down_load(content):
    #     下載圖片
    # urllib.request.urlretrieve('圖片地址','文件的名字')
    tree = etree.HTML(content)
    name_list = tree.xpath('//div[@id="container"]//a/img/@alt')
    # 一般設(shè)計(jì)圖片的網(wǎng)站都會進(jìn)行懶加載
    src_list = tree.xpath('//div[@id="container"]//a/img/@src2')
    print(src_list)
    for i in range(len(name_list)):
        name = name_list[i]
        src = src_list[i]
        url = 'https:' + src
        urllib.request.urlretrieve(url=url, filename='./loveImg/' + name + '.jpg')
if __name__ == '__main__':
    start_page = int(input('請輸入起始頁碼'))
    end_page = int(input('請輸入結(jié)束頁碼'))

    for page in range(start_page, end_page + 1):
        # (1) 請求對象的定制
        request = create_request(page)
        # (2)獲取網(wǎng)頁的源碼
        content = get_content(request)
        # (3)下載
        down_load(content)

2. JsonPath

2.1 pip安裝

pip install jsonpath 

2.2 jsonpath的使用

obj = json.load(open('json文件', 'r', encoding='utf‐8')) 
ret = jsonpath.jsonpath(obj, 'jsonpath語法') 

JSONPath語法元素和對應(yīng)XPath元素的對比:

示例:

jsonpath.json

{ "store": {
    "book": [
      { "category": "修真",
        "author": "六道",
        "title": "壞蛋是怎樣練成的",
        "price": 8.95
      },
      { "category": "修真",
        "author": "天蠶土豆",
        "title": "斗破蒼穹",
        "price": 12.99
      },
      { "category": "修真",
        "author": "唐家三少",
        "title": "斗羅大陸",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      { "category": "修真",
        "author": "南派三叔",
        "title": "星辰變",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "author": "老馬",
      "color": "黑色",
      "price": 19.95
    }
  }
}
import json
import jsonpath

obj = json.load(open('jsonpath.json', 'r', encoding='utf-8'))

# 書店所有書的作者
author_list = jsonpath.jsonpath(obj, '$.store.book[*].author')
print(author_list)  # ['六道', '天蠶土豆', '唐家三少', '南派三叔']

# 所有的作者
author_list = jsonpath.jsonpath(obj, '$..author')
print(author_list)  # ['六道', '天蠶土豆', '唐家三少', '南派三叔', '老馬']

# store下面的所有的元素
tag_list = jsonpath.jsonpath(obj, '$.store.*')
print(
    tag_list)  # [[{'category': '修真', 'author': '六道', 'title': '壞蛋是怎樣練成的', 'price': 8.95}, {'category': '修真', 'author': '天蠶土豆', 'title': '斗破蒼穹', 'price': 12.99}, {'category': '修真', 'author': '唐家三少', 'title': '斗羅大陸', 'isbn': '0-553-21311-3', 'price': 8.99}, {'category': '修真', 'author': '南派三叔', 'title': '星辰變', 'isbn': '0-395-19395-8', 'price': 22.99}], {'author': '老馬', 'color': '黑色', 'price': 19.95}]

# store里面所有東西的price
price_list = jsonpath.jsonpath(obj, '$.store..price')
print(price_list)  # [8.95, 12.99, 8.99, 22.99, 19.95]

# 第三個(gè)書
book = jsonpath.jsonpath(obj, '$..book[2]')
print(book)  # [{'category': '修真', 'author': '唐家三少', 'title': '斗羅大陸', 'isbn': '0-553-21311-3', 'price': 8.99}]

# 最后一本書
book = jsonpath.jsonpath(obj, '$..book[(@.length-1)]')
print(book)  # [{'category': '修真', 'author': '南派三叔', 'title': '星辰變', 'isbn': '0-395-19395-8', 'price': 22.99}]
# 	前面的兩本書
book_list = jsonpath.jsonpath(obj, '$..book[0,1]')
# book_list = jsonpath.jsonpath(obj,'$..book[:2]')
print(
    book_list)  # [{'category': '修真', 'author': '六道', 'title': '壞蛋是怎樣練成的', 'price': 8.95}, {'category': '修真', 'author': '天蠶土豆', 'title': '斗破蒼穹', 'price': 12.99}]

# 條件過濾需要在()的前面添加一個(gè)?
# 	 過濾出所有的包含isbn的書。
book_list = jsonpath.jsonpath(obj, '$..book[?(@.isbn)]')
print(
    book_list)  # [{'category': '修真', 'author': '唐家三少', 'title': '斗羅大陸', 'isbn': '0-553-21311-3', 'price': 8.99}, {'category': '修真', 'author': '南派三叔', 'title': '星辰變', 'isbn': '0-395-19395-8', 'price': 22.99}]
# 哪本書超過了10塊錢
book_list = jsonpath.jsonpath(obj, '$..book[?(@.price>10)]')
print(
    book_list)  # [{'category': '修真', 'author': '天蠶土豆', 'title': '斗破蒼穹', 'price': 12.99}, {'category': '修真', 'author': '南派三叔', 'title': '星辰變', 'isbn': '0-395-19395-8', 'price': 22.99}]

3. BeautifulSoup

3.1 基本簡介

1.安裝

 pip install bs4 

2.導(dǎo)入

 from bs4 import BeautifulSoup 

3.創(chuàng)建對象 

  •  服務(wù)器響應(yīng)的文件生成對象 soup = BeautifulSoup(response.read().decode(), 'lxml') 
  • 本地文件生成對象 soup = BeautifulSoup(open('1.html'), 'lxml') 

注意:默認(rèn)打開文件的編碼格式gbk所以需要指定打開編碼格式utf-8

3.2 安裝以及創(chuàng)建

1.根據(jù)標(biāo)簽名查找節(jié)點(diǎn) 
	soup.a 【注】只能找到第一個(gè)a 
		soup.a.name 
		soup.a.attrs 
2.函數(shù) 
	(1).find(返回一個(gè)對象) 
		find('a'):只找到第一個(gè)a標(biāo)簽
		find('a', title='名字') 
		find('a', class_='名字') 
	(2).find_all(返回一個(gè)列表) 
		find_all('a') 查找到所有的a 
		find_all(['a', 'span']) 返回所有的a和span 
		find_all('a', limit=2) 只找前兩個(gè)a 
	(3).select(根據(jù)選擇器得到節(jié)點(diǎn)對象)【推薦】 
		1.element 
			eg:p 
		2..class 
			eg:.firstname 
		3.#id
			eg:#firstname 
		4.屬性選擇器 
			[attribute] 
				eg:li = soup.select('li[class]') 
			[attribute=value] 
				eg:li = soup.select('li[class="hengheng1"]') 
		5.層級選擇器 
			element element 
				div p 
			element>element 
				div>p 
			element,element 
				div,p 
					eg:soup = soup.select('a,span')

3.3 節(jié)點(diǎn)定位

1.根據(jù)標(biāo)簽名查找節(jié)點(diǎn) 
	soup.a 【注】只能找到第一個(gè)a 
		soup.a.name 
		soup.a.attrs 
2.函數(shù) 
	(1).find(返回一個(gè)對象) 
		find('a'):只找到第一個(gè)a標(biāo)簽
		find('a', title='名字') 
		find('a', class_='名字') 
	(2).find_all(返回一個(gè)列表) 
		find_all('a') 查找到所有的a 
		find_all(['a', 'span']) 返回所有的a和span 
		find_all('a', limit=2) 只找前兩個(gè)a 
	(3).select(根據(jù)選擇器得到節(jié)點(diǎn)對象)【推薦】 
		1.element 
			eg:p 
		2..class 
			eg:.firstname 
		3.#id
			eg:#firstname 
		4.屬性選擇器 
			[attribute] 
				eg:li = soup.select('li[class]') 
			[attribute=value] 
				eg:li = soup.select('li[class="hengheng1"]') 
		5.層級選擇器 
			element element 
				div p 
			element>element 
				div>p 
			element,element 
				div,p 
					eg:soup = soup.select('a,span')

3.5 節(jié)點(diǎn)信息 

(1).獲取節(jié)點(diǎn)內(nèi)容:適用于標(biāo)簽中嵌套標(biāo)簽的結(jié)構(gòu) 
	obj.string 
	obj.get_text()【推薦】 
(2).節(jié)點(diǎn)的屬性 
	tag.name 獲取標(biāo)簽名 
		eg:tag = find('li) 
			print(tag.name) 
	tag.attrs將屬性值作為一個(gè)字典返回 
(3).獲取節(jié)點(diǎn)屬性 
	obj.attrs.get('title')【常用】 
	obj.get('title') 
	obj['title']
(1).獲取節(jié)點(diǎn)內(nèi)容:適用于標(biāo)簽中嵌套標(biāo)簽的結(jié)構(gòu) 
	obj.string 
	obj.get_text()【推薦】 
(2).節(jié)點(diǎn)的屬性 
	tag.name 獲取標(biāo)簽名 
		eg:tag = find('li) 
			print(tag.name) 
	tag.attrs將屬性值作為一個(gè)字典返回 
(3).獲取節(jié)點(diǎn)屬性 
	obj.attrs.get('title')【常用】 
	obj.get('title') 
	obj['title']

3.6 使用示例

bs4.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <div>
        <ul>
            <li id="l1">張三</li>
            <li id="l2">李四</li>
            <li>王五</li>
            <a href="" id=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" " class="a1">google</a>
            <span>嘿嘿嘿</span>
        </ul>
    </div>


    <a href="" title=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" a2">百度</a>

    <div id="d1">
        <span>
            哈哈哈
        </span>
    </div>

    <p id="p1" class="p1">呵呵呵</p>
</body>
</html>
from bs4 import BeautifulSoup
# 通過解析本地文件 來將bs4的基礎(chǔ)語法進(jìn)行講解
# 默認(rèn)打開的文件的編碼格式是gbk 所以在打開文件的時(shí)候需要指定編碼
soup = BeautifulSoup(open('bs4.html', encoding='utf-8'), 'lxml')
# 根據(jù)標(biāo)簽名查找節(jié)點(diǎn)
# 找到的是第一個(gè)符合條件的數(shù)據(jù)
print(soup.a)  # <a class="a1" href="" id=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ">google</a>
# 獲取標(biāo)簽的屬性和屬性值
print(soup.a.attrs)  # {'href': '', 'id': '', 'class': ['a1']}
# bs4的一些函數(shù)
# (1)find
# 返回的是第一個(gè)符合條件的數(shù)據(jù)
print(soup.find('a'))  # <a class="a1" href="" id=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ">google</a>
# 根據(jù)title的值來找到對應(yīng)的標(biāo)簽對象
print(soup.find('a', title="a2"))  # <a href="" title=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" a2">百度</a>

# 根據(jù)class的值來找到對應(yīng)的標(biāo)簽對象  注意的是class需要添加下劃線
print(soup.find('a', class_="a1"))  # <a class="a1" href="" id=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ">google</a>

# (2)find_all  返回的是一個(gè)列表 并且返回了所有的a標(biāo)簽
print(soup.find_all('a'))  # [<a class="a1" href="" id=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ">google</a>, <a href="" title=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" a2">百度</a>]

# 如果想獲取的是多個(gè)標(biāo)簽的數(shù)據(jù) 那么需要在find_all的參數(shù)中添加的是列表的數(shù)據(jù)
print(soup.find_all(['a','span']))  # [<a class="a1" href="" id=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ">google</a>, <span>嘿嘿嘿</span>, <a href="" title=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" a2">百</a><spa哈</span>]

# limit的作用是查找前幾個(gè)數(shù)據(jù)
print(soup.find_all('li', limit=2))  # [<li id="l1">張三</li>, <li id="l2">李四</li>]

# (3)select(推薦)
# select方法返回的是一個(gè)列表  并且會返回多個(gè)數(shù)據(jù)
print(soup.select('a'))  # [<a class="a1" href="" id=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ">google</a>, <a href="" title=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" a2">百度</a>]

# 可以通過.代表class  我們把這種操作叫做類選擇器
print(soup.select('.a1'))  # [<a class="a1" href="" id=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ">google</a>]

print(soup.select('#l1'))  # [<li id="l1">張三</li>]

# 屬性選擇器---通過屬性來尋找對應(yīng)的標(biāo)簽
# 查找到li標(biāo)簽中有id的標(biāo)簽
print(soup.select('li[id]'))  # [<li id="l1">張三</li>, <li id="l2">李四</li>]

# 查找到li標(biāo)簽中id為l2的標(biāo)簽
print(soup.select('li[id="l2"]'))  # [<li id="l2">李四</li>]

# 層級選擇器
#  后代選擇器
# 找到的是div下面的li
print(soup.select('div li'))  # [<li id="l1">張三</li>, <li id="l2">李四</li>, <li>王五</li>]

# 子代選擇器
#  某標(biāo)簽的第一級子標(biāo)簽
# 注意:很多的計(jì)算機(jī)編程語言中 如果不加空格不會輸出內(nèi)容  但是在bs4中 不會報(bào)錯(cuò) 會顯示內(nèi)容
print(soup.select('div > ul > li'))  # [<li id="l1">張三</li>, <li id="l2">李四</li>, <li>王五</li>]

# 找到a標(biāo)簽和li標(biāo)簽的所有的對象
print(soup.select(
    'a,li'))  # [<li id="l1">張三</li>, <li id="l2">李四</li>, <li>王五</li>, <a class="a1" href="" id=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" ">google</a>, <a href="" title=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" a2">百度</a>]

# 節(jié)點(diǎn)信息
#    獲取節(jié)點(diǎn)內(nèi)容
obj = soup.select('#d1')[0]
# 如果標(biāo)簽對象中 只有內(nèi)容 那么string和get_text()都可以使用
# 如果標(biāo)簽對象中 除了內(nèi)容還有標(biāo)簽 那么string就獲取不到數(shù)據(jù) 而get_text()是可以獲取數(shù)據(jù)
# 我們一般情況下  推薦使用get_text()
print(obj.string)  # None
print(obj.get_text())  # 哈哈哈

# 節(jié)點(diǎn)的屬性
obj = soup.select('#p1')[0]
# name是標(biāo)簽的名字
print(obj.name)  # p
# 將屬性值左右一個(gè)字典返回
print(obj.attrs)  # {'id': 'p1', 'class': ['p1']}

# 獲取節(jié)點(diǎn)的屬性
obj = soup.select('#p1')[0]
#
print(obj.attrs.get('class'))  # ['p1']
print(obj.get('class'))  # ['p1']
print(obj['class'])  # ['p1']

3.7 解析星巴克產(chǎn)品名稱

import urllib.request
url = 'https://www.starbucks.com.cn/menu/'
response = urllib.request.urlopen(url)
content = response.read().decode('utf-8')
from bs4 import BeautifulSoup
soup = BeautifulSoup(content,'lxml')
# //ul[@class="grid padded-3 product"]//strong/text()
# 一般先用xpath方式通過google插件寫好解析的表達(dá)式
name_list = soup.select('ul[class="grid padded-3 product"] strong')
for name in name_list:
    print(name.get_text())

到此這篇關(guān)于Python xpath,JsonPath,bs4的基本使用的文章就介紹到這了,更多相關(guān)Python xpath,JsonPath,bs4內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于python時(shí)間處理方法(詳解)

    基于python時(shí)間處理方法(詳解)

    下面小編就為大家?guī)硪黄趐ython時(shí)間處理方法(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • python3下載抖音視頻的完整代碼

    python3下載抖音視頻的完整代碼

    本文通過實(shí)例代碼給大家介紹了python3下載抖音視頻的相關(guān)知識,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-06-06
  • python區(qū)塊鏈地址的簡版實(shí)現(xiàn)

    python區(qū)塊鏈地址的簡版實(shí)現(xiàn)

    這篇文章主要為大家介紹了python區(qū)塊鏈地址的簡版實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • python實(shí)現(xiàn)的登陸Discuz!論壇通用代碼分享

    python實(shí)現(xiàn)的登陸Discuz!論壇通用代碼分享

    這篇文章主要介紹了python實(shí)現(xiàn)的登陸Discuz!論壇通用代碼分享,需要的朋友可以參考下
    2014-07-07
  • Python?如何實(shí)現(xiàn)變量交換

    Python?如何實(shí)現(xiàn)變量交換

    這篇文章主要介紹了Python?如何實(shí)現(xiàn)變量交換,Python?程序員肯定知道?a,b?=?b,a,這句話用來交換兩個(gè)變量。相較于其它語言需要引入一個(gè)?temp?來臨時(shí)存儲變量的做法,Python?的這種寫法無疑非常優(yōu)雅,下面我們來看看具體的實(shí)現(xiàn)過程吧
    2022-01-01
  • Python實(shí)現(xiàn)基于C/S架構(gòu)的聊天室功能詳解

    Python實(shí)現(xiàn)基于C/S架構(gòu)的聊天室功能詳解

    這篇文章主要介紹了Python實(shí)現(xiàn)基于C/S架構(gòu)的聊天室功能,結(jié)合實(shí)例形式詳細(xì)分析了Python實(shí)現(xiàn)聊天室功能的客戶端與服務(wù)器端相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下
    2018-07-07
  • 講解python參數(shù)和作用域的使用

    講解python參數(shù)和作用域的使用

    本文會介紹如何將語句組織成函數(shù),還會詳細(xì)介紹參數(shù)和作用域的概念,以及遞歸的概念及其在程序中的用途。
    2013-11-11
  • Python使用chardet判斷字符編碼

    Python使用chardet判斷字符編碼

    這篇文章主要介紹了Python使用chardet判斷字符編碼的方法,較為詳細(xì)的分析了Python中chardet的功能、安裝及使用技巧,需要的朋友可以參考下
    2015-05-05
  • python中讓自定義的類使用加號"+"

    python中讓自定義的類使用加號"+"

    這篇文章主要介紹了python中讓自定義的類使用加號"+",如果對兩個(gè)對象直接“+”肯定是不行的,因?yàn)檫€沒有對CartoonImage類重載加法運(yùn)算符__add__(),下文小編舉例形式講解該內(nèi)容,需要的下伙伴可以參考一下
    2022-03-03
  • Python生成掃雷地圖的方法

    Python生成掃雷地圖的方法

    這篇文章主要為大家詳細(xì)介紹了Python生成掃雷地圖的方法,并非游戲?qū)崿F(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09

最新評論