通往 CSS 定位器的(X)Path
很多人更喜歡用 CSS 定位器(CSS Locator)而不是 XPath。稍後你會看到,它常常能讓屬性的選取更容易。為了讓你更熟悉在 XPath 與 CSS 定位器字串之間來回切換,這個練習提供機會,讓你直接在兩者之間進行「翻譯」。
注意:本章的練習可能需要一點時間載入。
本練習屬於課程
Python 網頁爬蟲
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create the XPath string equivalent to the CSS Locator
xpath = '/html/body/span[1]//a'
# Create the CSS Locator string equivalent to the XPath
css_locator = ____