不走寻常的 XPath

Python Web 爬取

Thomas Laetsch

Data Scientist, NYU

(At)tribute

  • @ 表示"属性"
    • @class
    • @id
    • @href
Python Web 爬取

中括号与属性

xpathattr.png

Python Web 爬取

中括号与属性

xpathattr_div_p1.png

xpath = '//p[@class="class-1"]'
Python Web 爬取

中括号与属性

xpathattr_div.png

xpath = '//*[@id="uid"]'
Python Web 爬取

中括号与属性

xpathattr_div_astc2.png

xpath = '//div[@id="uid"]/p[2]'
Python Web 爬取

使用 contains 匹配内容

Xpath contains 语法:

contains( @属性名, "字符串表达式" )

Python Web 爬取

包含匹配

xpath = '//*[contains(@class,"class-1")]'

ClassSelection-Xpath-contains.png

Python Web 爬取

精确匹配

xpath = '//*[@class="class-1"]'

ClassSelection-Xpath-eq.png

Python Web 爬取

获取类名

xpathattr_div_astc2.png

xpath = '/html/body/div/p[2]'
Python Web 爬取

获取类名

xpathattr_div_p2-class.png

xpath = '/html/body/div/p[2]/@class'
Python Web 爬取

路径终点

Python Web 爬取

Preparing Video For Download...