scrape-cli 0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
scrape_cli/__init__.py ADDED
@@ -0,0 +1,11 @@
1
+ """
2
+ scrape-cli - A command-line tool to extract HTML elements using XPath or CSS3 selectors.
3
+ """
4
+
5
+ from scrape_cli.scrape import main
6
+
7
+ __version__ = "0.1"
8
+ __author__ = "Andrea Borruso"
9
+ __author_email__ = "aborruso@gmail.com"
10
+
11
+ __all__ = ['main']
scrape_cli/scrape.py ADDED
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ # scrape: Extract HTML elements using an XPath query or CSS3 selector.
4
+ #
5
+ # Example usage:
6
+ # $ curl 'http://en.wikipedia.org/wiki/List_of_sovereign_states' -s \
7
+ # | scrape -be 'table.wikitable > tr > td > b > a'
8
+ #
9
+ # Dependencies: lxml and cssselector
10
+ #
11
+ # Author: http://jeroenjanssens.com
12
+
13
+ import sys
14
+ import argparse
15
+ from lxml import etree
16
+ from cssselect import GenericTranslator
17
+ from sys import exit
18
+
19
+ def main():
20
+ parser = argparse.ArgumentParser(description='Extract HTML elements using an XPath query or CSS3 selector.')
21
+ parser.add_argument('html', nargs='?', type=argparse.FileType('rb'),
22
+ default=sys.stdin, help="HTML input (default: stdin)", metavar="HTML")
23
+ parser.add_argument('-a', '--argument', default="",
24
+ help="Argument to extract from tag")
25
+ parser.add_argument('-b', '--body', action='store_true', default=False,
26
+ help="Enclose output with HTML and BODY tags")
27
+ parser.add_argument('-e', '--expression', default=[], action='append',
28
+ help="XPath query or CSS3 selector")
29
+ parser.add_argument('-f', '--file', default='',
30
+ help="File to read input from")
31
+ parser.add_argument('-x', '--check-existence', action='store_true', default=False,
32
+ help="Process return value signifying existence")
33
+ parser.add_argument('-r', '--rawinput', action='store_true', default=False,
34
+ help="Do not parse HTML before feeding etree (useful for escaping CData)")
35
+ args = parser.parse_args()
36
+
37
+ # Check if the required arguments are provided
38
+ if not args.expression:
39
+ print("Error: You must provide at least one XPath query or CSS3 selector using the -e option.")
40
+ parser.print_help()
41
+ sys.exit(1)
42
+
43
+ expression = [e if e.startswith('//') else GenericTranslator().css_to_xpath(e) for e in args.expression]
44
+
45
+ html_parser = etree.HTMLParser(encoding='utf-8', recover=True, strip_cdata=True)
46
+
47
+ inp = open(args.file, 'rb') if args.file else args.html
48
+ if args.rawinput:
49
+ document = etree.fromstring(inp.read())
50
+ else:
51
+ document = etree.parse(inp, html_parser)
52
+
53
+ if args.body:
54
+ sys.stdout.write("<!DOCTYPE html>\n<html>\n<body>\n")
55
+
56
+ for e in expression:
57
+ els = list(document.xpath(e))
58
+
59
+ if args.check_existence:
60
+ sys.exit(1 if len(els) == 0 else 0)
61
+
62
+ for e in els:
63
+ if isinstance(e, str):
64
+ text = e
65
+ elif not args.argument:
66
+ text = etree.tostring(e, pretty_print=True).decode('utf-8')
67
+ else:
68
+ text = e.get(args.argument)
69
+ if text is not None:
70
+ sys.stdout.write(text.strip() + "\n")
71
+
72
+ if args.body:
73
+ sys.stdout.write("</body>\n</html>")
74
+
75
+ sys.stdout.write('\n')
76
+ sys.stdout.flush()
77
+
78
+ if __name__ == "__main__":
79
+ exit(main())
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Andrea Borruso
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,15 @@
1
+ Metadata-Version: 2.1
2
+ Name: scrape-cli
3
+ Version: 0.1
4
+ Summary: It's a command-line tool to extract HTML elements using an XPath query or CSS3 selector.
5
+ Home-page: https://github.com/aborruso/scrape-cli
6
+ Author: Andrea Borruso
7
+ Author-email: aborruso@gmail.com
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.6
12
+ License-File: LICENSE
13
+ Requires-Dist: cssselect
14
+ Requires-Dist: lxml
15
+
@@ -0,0 +1,8 @@
1
+ scrape_cli/__init__.py,sha256=vMovgMhMpQHcvqmEz9zWJPKAnOpIQF_uLKwyeoMaW1I,244
2
+ scrape_cli/scrape.py,sha256=Q8IuxtI3nqp0Nav2iQLNqgdolEItVkVi4yl_xv_xh-Q,2969
3
+ scrape_cli-0.1.dist-info/LICENSE,sha256=VdJfydYDIXcUkSoEOEnxnLebI5U1QuJi_4qovlfn9Qo,1071
4
+ scrape_cli-0.1.dist-info/METADATA,sha256=WHty5-GUpl3qU_vZKsFoJmN195FUP2SKRWQV9oU-lAc,493
5
+ scrape_cli-0.1.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
6
+ scrape_cli-0.1.dist-info/entry_points.txt,sha256=WiInKTUuhXjG42gk92XozBIYrKo-dSFbqwvIDerwTrA,50
7
+ scrape_cli-0.1.dist-info/top_level.txt,sha256=IZ1nsWomaPLUPQp2RawfhcXnaJ6_C2vL8-dXf9YQXDE,11
8
+ scrape_cli-0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.3.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ scrape = scrape_cli.scrape:main
@@ -0,0 +1 @@
1
+ scrape_cli