Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
eautiful Soup 包:
Beautiful Soup: Python 的第三方插件用來提取 xml 和 HTML 中的數(shù)據(jù)。官網(wǎng)地址 https://www.crummy.com/software/BeautifulSoup/
1、安裝 Beautiful Soup
打開 cmd(命令提示符),進(jìn)入到 Python(Python2.7版本)安裝目錄中的 scripts 下,輸入 dir 查看是否有 pip.exe, 如果用就可以使用 Python 自帶的 pip 命令進(jìn)行安裝,輸入以下命令進(jìn)行安裝即可:
pip install beautifulsoup4
2、測(cè)試是否安裝成功
編寫一個(gè) Python 文件,輸入:
import bs4
print bs4
運(yùn)行該文件,如果能夠正常輸出則安裝成功。
五、使用 Beautiful Soup 解析 html 文件
# -*- coding: UTF-8 -*-
import bs4
import re
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
# 創(chuàng)建一個(gè)BeautifulSoup解析對(duì)象
soup = BeautifulSoup(html_doc, "html.parser", from_encoding="utf-8")
# 獲取所有的鏈接
links = soup.find_all('a')
print("所有的鏈接")
for link in links:
print(link.name, link['href'], link.get_text())
print("獲取特定的URL地址")
link_node = soup.find('a', href="http://example.com/elsie")
print(link_node.name, link_node['href'], link_node['class'], link_node.get_text())
print("正則表達(dá)式匹配")
link_node = soup.find('a', href=re.compile(r"ti"))
print(link_node.name, link_node['href'], link_node['class'], link_node.get_text())
print("獲取P段落的文字")
p_node = soup.find('p', class_='story')
print(p_node.name, p_node['class'], p_node.get_text())
===========
輸出:
isual Studio Code 是一款輕量級(jí)但功能強(qiáng)大的源代碼編輯器,適用于 Windows、macOS 和 Linux。內(nèi)置了對(duì) JavaScript、TypeScript 和 Node.js 的支持,并為其他語言(例如 C++、C#、Java、Python、PHP、Go)并且提供了豐富的擴(kuò)展生態(tài)系統(tǒng)。
今天我將給大家介紹下VS Code常用操作及非常棒的插件,喜歡本文記得收藏、點(diǎn)贊、關(guān)注。
關(guān)于VScode歷史推文一并總結(jié)如下:
廢話不多說,我們開始吧!
使用教程
1.搜索擴(kuò)展
您可以清除“擴(kuò)展”視圖頂部的“搜索”框,然后輸入您要查找的擴(kuò)展、工具或編程語言的名稱。
2.列出已安裝的擴(kuò)展
默認(rèn)“擴(kuò)展”視圖顯示當(dāng)前的擴(kuò)展、推薦的所有擴(kuò)展以及已禁用的所有擴(kuò)展的折疊視圖。可以使用命令面板( Ctrl+Shift+P ) 命令清除搜索框中的任何文本并顯示所有已安裝擴(kuò)展的列表。
3.卸載擴(kuò)展
要卸載擴(kuò)展,請(qǐng)選擇擴(kuò)展條目右側(cè)的管理齒輪按鈕,然后從下拉菜單中選擇卸載。可以卸載擴(kuò)展并提示您重新加載 VS Code。
4.禁用擴(kuò)展
如果不想永久刪除擴(kuò)展程序,可以通過單擊擴(kuò)展程序條目右側(cè)的齒輪按鈕暫時(shí)禁用擴(kuò)展程序。
在全局禁用擴(kuò)展或僅針對(duì)當(dāng)前工作區(qū)禁用擴(kuò)展。禁用擴(kuò)展后,系統(tǒng)將提示重新加載 VS Code。
5.啟用擴(kuò)展
如果禁用了某個(gè)擴(kuò)展程序,可以使用下拉菜單中的Enable或Enable (Workspace)命令重新啟用它。
6.更新擴(kuò)展
自動(dòng)更新:VS Code 檢查擴(kuò)展更新并自動(dòng)安裝。更新后,系統(tǒng)會(huì)提示您重新加載 VS Code。
手動(dòng)更新:使用Show Outdated Extensions命令快速查找擴(kuò)展更新,顯示當(dāng)前安裝的擴(kuò)展程序的所有可用更新,為過時(shí)的擴(kuò)展選擇更新按鈕,更新將被安裝,系統(tǒng)將提示重新加載 VS Code。
擴(kuò)展介紹
1.Python
對(duì)Python 語言有豐富的支持,包括 IntelliSense (Pylance)、linting、調(diào)試、代碼導(dǎo)航、代碼格式化、重構(gòu)、變量瀏覽器等功能、測(cè)試資源管理器等。
2.Jupyter
它提供基本的筆記本支持語言內(nèi)核所支持許多語言內(nèi)核無需修改即可工作。默認(rèn)Jupyter 擴(kuò)展包括 Jupyter Keymaps 和 Jupyter Notebook Renderers 擴(kuò)展。
3.C/C++
C/C++ 擴(kuò)展為 Visual Studio Code 添加了對(duì) C/C++ 的語言支持,包括 IntelliSense 和調(diào)試等功能。
4.ESLint
ESLint 是一種用于識(shí)別和報(bào)告在 ECMAScript/JavaScript 代碼中發(fā)現(xiàn)的模式的工具,其目標(biāo)是使代碼更加一致并避免錯(cuò)誤。在許多方面,它類似于 JSLint 和 JSHint。
5.Prettier
Prettier支持我們大前端目前大部分語言處理,包括 JavaScript · Flow · TypeScript · CSS · SCSS · Less · JSX · Vue · GraphQL · JSON · Markdown,這代表著, 你幾乎可以用一個(gè)工具都能搞定所有的代碼格式化問題。
6.Live Server
為靜態(tài)和動(dòng)態(tài)頁面啟動(dòng)具有實(shí)時(shí)重新加載功能的開發(fā)本地服務(wù)器
7.Visual Studio IntelliCode
提供了為Python,TypeScript/ JavaScript和Java開發(fā)AI輔助開發(fā)功能在Visual Studio代碼,基于代碼的上下文與機(jī)器學(xué)習(xí)相結(jié)合。
8.Code Runner
運(yùn)行多種語言的代碼片段或代碼文件,省掉保存的環(huán)節(jié),直接可以運(yùn)行
9.Chinese (Simplified) (簡(jiǎn)體中文)
為VS Code 使用者提供漢字化界面,針對(duì)英文界面使用不太熟練的使用者
10.background
平時(shí)編程總是對(duì)著一頓枯燥的代碼背景,VSCode一個(gè)神仙插件Background,可以更改自己想要的背景圖,下面來介紹一波。
具體實(shí)現(xiàn)效果圖:
背景圖放最上面了,之前寫過一篇文章。
11.Auto Rename Tag
自動(dòng)重命名配對(duì)的 HTML/XML 標(biāo)簽,與 Visual Studio IDE 相同。
12.Better Comments
Better Comments 擴(kuò)展將幫助您在代碼中創(chuàng)建更人性化的注釋。使用此擴(kuò)展程序,您將能夠?qū)⒆⑨尫诸悶椋?/span>
13.Draw io Integration
在VSCode中就可以創(chuàng)建思維導(dǎo)圖,要?jiǎng)?chuàng)建新圖表,只需創(chuàng)建一個(gè)空的*.drawio,*.drawio.svg或*.drawio.png文件并打開它drawio.svg、.drawio.png嵌入到 Github,不需要導(dǎo)出。
14.Markdown emoji
向 VS Code 的內(nèi)置 Markdown 預(yù)覽添加:emoji:語法支持 ,生成表情
15.WakaTime
直觀的反應(yīng)你一天學(xué)習(xí)打代碼的時(shí)間,通過各種圖表計(jì)算
插件列表網(wǎng)址:https://plugincompat.herokuapp.com 包含很多插件包,大家可依據(jù)工作的需求選擇使用。
前置條件
1.文件路徑:
- Test_App
- - test_abc.py
- - pytest.ini
2.pyetst.ini配置文件內(nèi)容:
[pytest]
# 命令行參數(shù)
addopts = -s
# 搜索文件名
python_files = test_*.py
# 搜索的類名
python_classes = Test_*
#搜索的函數(shù)名
python_functions = test_*
pytest-HTML是一個(gè)插件,pytest用于生成測(cè)試結(jié)果的HTML報(bào)告。兼容Python 2.7,3.6
安裝方式:pip install pytest-html
pip install pytest-html
通過命令行方式,生成xml/html格式的測(cè)試報(bào)告,存儲(chǔ)于用戶指定路徑。插件名稱:pytest-html
使用方法: 命令行格式:pytest --html=用戶路徑/report.html
示例:
import pytest
class TestDemo():
def setup_class(self):
print("------->setup_class")
def teardown_class(self):
print("------->teardown_class")
def test_a(self):
print("------->test_a")
assert 1
def test_b(self):
print("------->test_b")
assert 0 # 斷言失敗```
運(yùn)行方式:
1.修改Test_App/pytest.ini文件,添加報(bào)告參數(shù),即:addopts = -s --html=./report.html
-s:輸出程序運(yùn)行信息
--html=./report.html 在當(dāng)前目錄下生成report.html文件 ? 若要生成xml文件,可將--html=./report.html 改成 --html=./report.xml
2.命令行進(jìn)入Test_App目錄 3.執(zhí)行命令: pytest 執(zhí)行結(jié)果: 1.在當(dāng)前目錄會(huì)生成assets文件夾和report.html文件
失敗重試意思是指定某個(gè)用例執(zhí)行失敗可以重新運(yùn)行。
下載安裝
pip install pytest-rerunfailures
使用
需要在pytest.ini文件中,給addopts字段新增(其他原有保持不變)--reruns=3字段,這樣如果有用例執(zhí)行失敗,則再次執(zhí)行,嘗試3次。 配置:
[pytest]
addopts = -s --html=report/report.html --reruns=3
代碼實(shí)例如下:
import pytest
def test_case01():
print('執(zhí)行用例01.......')
assert 1 # 斷言成功
def test_case02():
print('執(zhí)行用例02.......')
assert 0 # 斷言失敗,需要重新執(zhí)行
class TestCaseClass(object):
def test_case_03(self):
print('執(zhí)行用例03.......')
assert 1
我們也可以從用例報(bào)告中看出重試的結(jié)果。
失敗重試包括兩種情況:一種情況:用例失敗了,然后重新執(zhí)行多少次都沒有成功。另一種情況,那就是用例執(zhí)行失敗,重新執(zhí)行次數(shù)內(nèi)通過了,那么剩余的重新執(zhí)行的次數(shù)將不再執(zhí)行。
如何手動(dòng)控制多個(gè)用例的執(zhí)行順序,這里也依賴一個(gè)插件。
下載安裝
>pip install pytest-ordering
使用實(shí)例
手動(dòng)控制用例執(zhí)行順序的方法是在給各用例添加一個(gè)裝飾器:
@pytest.mark.run(order=x) # x 是一個(gè)整數(shù)
代碼如下:
import pytest
class TestCaseClass(object):
@pytest.mark.run(order=3)
def test_case_03(self):
print('執(zhí)行用例03.......')
assert 1
@pytest.mark.run(order=2)
def test_case01():
print('執(zhí)行用例01.......')
assert 1 # 斷言成功
@pytest.mark.run(order=1)
def test_case02():
print('執(zhí)行用例02.......')
assert 1 # 斷言成功
那么, 現(xiàn)在的執(zhí)行順序是2 1 3,按照order指定的排序執(zhí)行的。
如果傳個(gè)0或者負(fù)數(shù)啥的,那么它們的排序關(guān)系應(yīng)該是這樣的:
0 > 正數(shù) > 沒有參與的用例 > 負(fù)數(shù)
# 正數(shù)和負(fù)數(shù)就是按照大小關(guān)系排列的
一條一條用例的執(zhí)行,肯定會(huì)很慢,來看如何并發(fā)的執(zhí)行測(cè)試用例,當(dāng)然這需要相應(yīng)的插件
下載安裝
pip install pytest-xdist
使用
在pytest.ini 配置文件中addopts 添加 -n=auto , 修改配置如下:
[pytest]
addopts = -v -s --html=report/report.html -n=auto
就是這個(gè)-n=auto:
并發(fā)的配置可以寫在配置文件中,然后其他正常的執(zhí)行用例腳本即可
import pytest
def test_case01():
print('執(zhí)行用例01.......')
assert 1 # 斷言成功
@pytest.mark.skipif(condition= 2 > 1, reason='跳過用例')
def test_case02():
print('執(zhí)行用例02.......')
assert 0 # 斷言失敗
class TestCaseClass(object):
def test_case_03(self):
print('執(zhí)行用例03.......')
assert 1
def test_case_04(self):
print('執(zhí)行用例04.......')
assert 1
pytest-sugar 改變了 pytest 的默認(rèn)外觀,添加了一個(gè)進(jìn)度條,并立即顯示失敗的測(cè)試。它不需要配置,只需 下載插件即可,用 pytest 運(yùn)行測(cè)試,來享受更漂亮、更有用的輸出。
安裝下載
pip install pytest-sugar
其他照舊執(zhí)行用例即可。
pytest-cov 在 pytest 中增加了覆蓋率支持,來顯示哪些代碼行已經(jīng)測(cè)試過,哪些還沒有。它還將包括項(xiàng)目的測(cè)試覆蓋率。
下載
pip install pytest-cov
使用
在配置文件pytest.ini中配置--cov=./scripts ,這樣,它就會(huì)統(tǒng)計(jì)所有scripts目錄下所有符合規(guī)則的腳本的測(cè)試覆蓋率。
配置修改如下:
[pytest]
addopts = -v -s --html=report/report.html -n=auto --cov=./scripts
執(zhí)行的話,就照常執(zhí)行就行。
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。