scrape-cli 0.1__tar.gz
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-0.1/LICENSE +21 -0
- scrape_cli-0.1/PKG-INFO +14 -0
- scrape_cli-0.1/README.md +92 -0
- scrape_cli-0.1/scrape_cli/__init__.py +11 -0
- scrape_cli-0.1/scrape_cli/scrape.py +79 -0
- scrape_cli-0.1/scrape_cli.egg-info/PKG-INFO +14 -0
- scrape_cli-0.1/scrape_cli.egg-info/SOURCES.txt +11 -0
- scrape_cli-0.1/scrape_cli.egg-info/dependency_links.txt +1 -0
- scrape_cli-0.1/scrape_cli.egg-info/entry_points.txt +2 -0
- scrape_cli-0.1/scrape_cli.egg-info/requires.txt +2 -0
- scrape_cli-0.1/scrape_cli.egg-info/top_level.txt +1 -0
- scrape_cli-0.1/setup.cfg +4 -0
- scrape_cli-0.1/setup.py +26 -0
scrape_cli-0.1/LICENSE
ADDED
|
@@ -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.
|
scrape_cli-0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
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
|
scrape_cli-0.1/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# scrape cli
|
|
2
|
+
|
|
3
|
+
It's a **command-line tool** to **extract** HTML elements using an [**XPath**](https://www.w3schools.com/xml/xpath_intro.asp) query or [**CSS3 selector**](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors).
|
|
4
|
+
|
|
5
|
+
It's based on the great and simple [scraping tool](https://github.com/jeroenjanssens/data-science-at-the-command-line/blob/master/tools/scrape) written by [**Jeroen Janssens**](http://jeroenjanssens.com).
|
|
6
|
+
|
|
7
|
+
- [How does it work?](#how-does-it-work)
|
|
8
|
+
- [How to use it in Linux](#how-to-use-it-in-linux)
|
|
9
|
+
- [Note on building it](#note-on-building-it)
|
|
10
|
+
|
|
11
|
+
## How does it work?
|
|
12
|
+
|
|
13
|
+
A CSS selector query like this
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
curl -L 'https://en.wikipedia.org/wiki/List_of_sovereign_states' -s \
|
|
17
|
+
| scrape -be 'table.wikitable > tbody > tr > td > b > a'
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
or an XPATH query like this one:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
curl -L 'https://en.wikipedia.org/wiki/List_of_sovereign_states' -s \
|
|
24
|
+
| scrape -be '//table[contains(@class, 'wikitable')]/tbody/tr/td/b/a'
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
gives you back:
|
|
28
|
+
|
|
29
|
+
```html
|
|
30
|
+
<html>
|
|
31
|
+
<head>
|
|
32
|
+
</head>
|
|
33
|
+
<body>
|
|
34
|
+
<a href="/wiki/Afghanistan" title="Afghanistan">
|
|
35
|
+
Afghanistan
|
|
36
|
+
</a>
|
|
37
|
+
<a href="/wiki/Albania" title="Albania">
|
|
38
|
+
Albania
|
|
39
|
+
</a>
|
|
40
|
+
<a href="/wiki/Algeria" title="Algeria">
|
|
41
|
+
Algeria
|
|
42
|
+
</a>
|
|
43
|
+
<a href="/wiki/Andorra" title="Andorra">
|
|
44
|
+
Andorra
|
|
45
|
+
</a>
|
|
46
|
+
<a href="/wiki/Angola" title="Angola">
|
|
47
|
+
Angola
|
|
48
|
+
</a>
|
|
49
|
+
<a href="/wiki/Antigua_and_Barbuda" title="Antigua and Barbuda">
|
|
50
|
+
Antigua and Barbuda
|
|
51
|
+
</a>
|
|
52
|
+
<a href="/wiki/Argentina" title="Argentina">
|
|
53
|
+
Argentina
|
|
54
|
+
</a>
|
|
55
|
+
<a href="/wiki/Armenia" title="Armenia">
|
|
56
|
+
Armenia
|
|
57
|
+
</a>
|
|
58
|
+
...
|
|
59
|
+
...
|
|
60
|
+
</body>
|
|
61
|
+
</html>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Some notes on the commands:
|
|
65
|
+
|
|
66
|
+
- `-e` to set the query
|
|
67
|
+
- `-b` to add `<html>`, `<head>` and `<body>` tags to the HTML output.
|
|
68
|
+
|
|
69
|
+
## How to use it in Linux
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
# go in example to the home folder
|
|
73
|
+
cd ~
|
|
74
|
+
# download scrape-cli
|
|
75
|
+
wget "https://github.com/aborruso/scrape-cli/releases/download/v1.0/scrape"
|
|
76
|
+
# move it in a folder of your PATH as /usr/bin
|
|
77
|
+
sudo mv ./scrape /usr/bin
|
|
78
|
+
# give it execute permission
|
|
79
|
+
sudo chmod +x /usr/bin/scrape
|
|
80
|
+
# use it
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
**Please note**: in OSX it seems not to work ([#8](https://github.com/aborruso/scrape-cli/issues/8)).
|
|
84
|
+
|
|
85
|
+
## Note on building it
|
|
86
|
+
|
|
87
|
+
The original source is written in Python 2, then I have built it in Python 2 environment.<br>
|
|
88
|
+
There are two modules requirements: install in this environment `cssselect` and then `lxml`, in this order (using pip).
|
|
89
|
+
|
|
90
|
+
I have built it using [pyinstaller](https://www.pyinstaller.org/) and this command: `pyinstaller --onefile scrape.py`.<br>
|
|
91
|
+
|
|
92
|
+
Once you have built it, it's an executable, and it's possible to use it in any environment.
|
|
@@ -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']
|
|
@@ -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,14 @@
|
|
|
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
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
setup.py
|
|
4
|
+
scrape_cli/__init__.py
|
|
5
|
+
scrape_cli/scrape.py
|
|
6
|
+
scrape_cli.egg-info/PKG-INFO
|
|
7
|
+
scrape_cli.egg-info/SOURCES.txt
|
|
8
|
+
scrape_cli.egg-info/dependency_links.txt
|
|
9
|
+
scrape_cli.egg-info/entry_points.txt
|
|
10
|
+
scrape_cli.egg-info/requires.txt
|
|
11
|
+
scrape_cli.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
scrape_cli
|
scrape_cli-0.1/setup.cfg
ADDED
scrape_cli-0.1/setup.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from setuptools import setup
|
|
2
|
+
|
|
3
|
+
setup(
|
|
4
|
+
name="scrape-cli",
|
|
5
|
+
version="0.1",
|
|
6
|
+
description="It's a command-line tool to extract HTML elements using an XPath query or CSS3 selector.",
|
|
7
|
+
author="Andrea Borruso",
|
|
8
|
+
author_email="aborruso@gmail.com",
|
|
9
|
+
url="https://github.com/aborruso/scrape-cli",
|
|
10
|
+
packages=["scrape_cli"], # nota: usa underscore invece di trattino
|
|
11
|
+
entry_points={
|
|
12
|
+
'console_scripts': [
|
|
13
|
+
'scrape=scrape_cli.scrape:main',
|
|
14
|
+
],
|
|
15
|
+
},
|
|
16
|
+
install_requires=[
|
|
17
|
+
"cssselect",
|
|
18
|
+
"lxml"
|
|
19
|
+
],
|
|
20
|
+
classifiers=[
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"License :: OSI Approved :: MIT License",
|
|
23
|
+
"Operating System :: OS Independent",
|
|
24
|
+
],
|
|
25
|
+
python_requires='>=3.6',
|
|
26
|
+
)
|