forked from gil9red/SimplePyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
39 lines (28 loc) · 890 Bytes
/
Copy pathexamples.py
File metadata and controls
39 lines (28 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
# pip install cssselect
from cssselect import HTMLTranslator
css_to_xpath = HTMLTranslator(xhtml=True).css_to_xpath
if __name__ == "__main__":
xpath_expr = css_to_xpath("div#main > a[href]")
print(xpath_expr) # descendant-or-self::div[@id = 'main']/a[@href]
xpath_expr = css_to_xpath("div")
print(xpath_expr) # descendant-or-self::div
xpath_expr = css_to_xpath("table:nth-last-child(1)")
print(xpath_expr) # descendant-or-self::table[count(following-sibling::*) = 0]
print()
for item in (
"#title",
"#head",
"#heading",
".pageTitle",
".news_title",
".title",
".head",
".heading",
".contentheading",
".small_header_red",
):
xpath_expr = css_to_xpath(item)
print(xpath_expr)