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