通往 CSS 定位器的 (X)Path
许多人更喜欢使用 CSS 定位器表示法而不是 XPath 表示法。正如我们稍后会看到的,它常常能让属性选择变得更简单。为了帮助您熟悉在 XPath 与 CSS 定位器字符串之间来回切换,本练习为您提供一次在两者之间直接"翻译"的机会。
注意:本章中的练习可能需要一些时间加载。
本练习是课程的一部分
Python Web 爬取
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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 = ____