溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python爬蟲神器playwright怎么使用

發(fā)布時間:2023-04-13 14:59:05 來源:億速云 閱讀:156 作者:iii 欄目:編程語言

這篇文章主要介紹了Python爬蟲神器playwright怎么使用的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Python爬蟲神器playwright怎么使用文章都會有所收獲,下面我們一起來看看吧。

前言

今天把最近的一個應(yīng)用做好了,測試了一下運(yùn)行沒有問題,剩下的就是檢驗(yàn)一下結(jié)果如何.從光譜到Lab值通常使用matlab中的roo2lab(),不過經(jīng)過我最近的測試發(fā)現(xiàn)轉(zhuǎn)換的結(jié)果并不理想,而且這個轉(zhuǎn)化的代碼也不是我寫的所以另尋他法,找到了下面這個網(wǎng)頁。

Python爬蟲神器playwright怎么使用

動手

有了這個網(wǎng)頁,很簡單就想到去解析.然后很快找到了這個api,可以看到用post提交表單請求就可以返回結(jié)果。

Python爬蟲神器playwright怎么使用

于是一番操作,修改form表單,構(gòu)造傳入的spectrum,但是最終請求得到的卻是一個頁面,并不是想要的json.然后許久沒寫爬蟲的我直接惱火,想到自動化工具模擬操作.可是selenium很難用而且還得去找瀏覽器新版本的驅(qū)動,隨后直接搜索一番,發(fā)現(xiàn)了這個神器—playwright。

首先老規(guī)矩去它的首頁看看教程

Python爬蟲神器playwright怎么使用

接著安裝一下playwright以及瀏覽器的驅(qū)動。

pip install pytest-playwright
playwright install

第二步的時候很慢,所以我只等他下載好了chrome和ffmpeg直接就ctrl+c停止了,畢竟我也用不著其他的瀏覽器驅(qū)動。

然后稍微瀏覽一下這個文檔功能非常豐富,不過我用到的功能也不多,接下來的使用才是真正適合我們懶人的。

記住一行代碼
python -m playwright codegen xxx.com

其中xxx.com就是我們的目標(biāo)網(wǎng)址,運(yùn)行后會創(chuàng)建一個熟悉的自動化頁面,然后我們就進(jìn)行一些我們想要的操作,比如設(shè)置開始的最小波長為400nm,然后觀察以及光源改為D65/10.一系列操作后會看到對應(yīng)的代碼已經(jīng)生成好了。

Python爬蟲神器playwright怎么使用

至今為止我還沒有寫一行代碼,不過構(gòu)造輸入的光譜還是得自己來的。

把這一段代碼復(fù)制下來,然后自己寫構(gòu)造輸入的函數(shù)(就幾行),然后通過選擇器(直接左鍵元素復(fù)制xpath)得到lab值,這樣目標(biāo)就搞定了。

整體代碼如下:

import time
from playwright.sync_api import Playwright, sync_playwright, expect
import numpy as np
data_test=np.loadtxt('./dist/1_res.csv',delimiter=',')
def get_str(arr):
arr_str=""
for i in arr:
arr_str+=str(format(i,".2f"))+"rn"
return arr_str
labs=[]
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
# Open new page
page = context.new_page()
# Go to https://www.qtccolor.com/secaiku/tool/spectrum
page.goto("https://www.qtccolor.com/secaiku/tool/spectrum")
# Click div[role="tab"]:has-text("光譜數(shù)據(jù)")
page.locator("div[role="tab"]:has-text("光譜數(shù)據(jù)")").click(
# Click text=最小波長:nm
page.locator("text=最小波長:nm").click()
# Fill [placeholder="33 80"]
page.locator("[placeholder="\33 80"]").fill("400")
# Select 1964
page.locator("select[name="obs"]").select_option("1964")
# Select D65
page.locator("select[name="ill"]").select_option("D65")
# Fill textarea[name="spectrum"]
for i in range(len(data_test)):
inputs=get_str(data_test[i,:])
# Click textarea[name="spectrum"]
page.locator("textarea[name="spectrum"]").click()
page.locator("textarea[name="spectrum"]").press("Control+a")
page.locator("textarea[name="spectrum"]").fill(inputs)
# Click button:has-text("轉(zhuǎn)換顏色")
page.locator("button:has-text("轉(zhuǎn)換顏色")").click()
time.sleep(1)
# Click text=Lab0.000.000.00 >> td >> nth=1
L=float(page.locator('xpath=//*[@]/div[1]/div/div[2]/table/tbody/tr[2]/td[2]').inner_text())
# Click text=Lab0.000.000.00 >> td >> nth=2
a=float(page.locator('xpath=//*[@]/div[1]/div/div[2]/table/tbody/tr[2]/td[3]').inner_text())
# Click text=Lab0.000.000.00 >> td >> nth=3
b=float(page.locator('xpath=//*[@]/div[1]/div/div[2]/table/tbody/tr[2]/td[4]').inner_text())
print(L,a,b)
labs.append([L,a,b])
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
np.savetxt('./1_lab_res.csv',labs,delimiter=",")

可以說從安裝到實(shí)現(xiàn)就幾分鐘,而且特別容易上手,我第一次用也一下就能實(shí)現(xiàn)效果。

Python爬蟲神器playwright怎么使用

剩下的就是簡單的寫個函數(shù)計(jì)算色差啥的就沒難度了。

關(guān)于“Python爬蟲神器playwright怎么使用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“Python爬蟲神器playwright怎么使用”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI