scrape-cli 1.3.3__tar.gz → 1.3.4__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scrape_cli
3
- Version: 1.3.3
3
+ Version: 1.3.4
4
4
  Summary: It's a command-line tool to extract HTML elements using an XPath query or CSS3 selector.
5
5
  Author-email: Andrea Borruso <aborruso@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/aborruso/scrape-cli
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "scrape_cli"
7
- version = "1.3.3"
7
+ version = "1.3.4"
8
8
  description = "It's a command-line tool to extract HTML elements using an XPath query or CSS3 selector."
9
9
  readme = "README.md"
10
10
  authors = [
@@ -27,6 +27,7 @@ Homepage = "https://github.com/aborruso/scrape-cli"
27
27
 
28
28
  [project.scripts]
29
29
  scrape = "scrape_cli.scrape:main"
30
+ scrape-cli = "scrape_cli.scrape:main"
30
31
 
31
32
  [tool.setuptools.packages.find]
32
33
  where = ["."]
@@ -4,7 +4,7 @@ scrape-cli - A command-line tool to extract HTML elements using XPath or CSS3 se
4
4
 
5
5
  from scrape_cli.scrape import main
6
6
 
7
- __version__ = "1.3.3"
7
+ __version__ = "1.3.4"
8
8
  __author__ = "Andrea Borruso"
9
9
  __author_email__ = "aborruso@gmail.com"
10
10
 
@@ -162,8 +162,8 @@ notes:
162
162
  - -j fails with exit 1 on attribute names that are not valid XML, such as
163
163
  @click or :bind. Use -e without -j on those pages.
164
164
  - URLs: 30 s timeout, HTTP errors (4xx, 5xx) exit 1.
165
- - Charset: from the meta tag, otherwise UTF-8. Other encodings need the meta
166
- tag, or characters come out wrong.
165
+ - Charset: from the meta tag, otherwise UTF-8, and ISO-8859-1 if the bytes
166
+ are not valid UTF-8. Other encodings need the meta tag.
167
167
  '''
168
168
  )
169
169
 
@@ -299,19 +299,16 @@ notes:
299
299
  # If detected charset fails, fall back to default behavior
300
300
  pass
301
301
 
302
- # Try UTF-8 first, fallback to ISO-8859-1 if that fails
303
- try:
304
- if args.rawinput:
305
- document = etree.fromstring(inp)
306
- else:
307
- document = etree.fromstring(inp, html_parser)
308
- except UnicodeDecodeError:
309
- # If UTF-8 fails, try ISO-8859-1
310
- inp = inp.decode('iso-8859-1').encode('utf-8')
311
- if args.rawinput:
312
- document = etree.fromstring(inp)
313
- else:
314
- document = etree.fromstring(inp, html_parser)
302
+ if args.rawinput:
303
+ # XML declares its own encoding: leave the bytes to the parser
304
+ document = etree.fromstring(inp)
305
+ else:
306
+ # The recovering parser never fails on bad bytes, so check them here
307
+ try:
308
+ inp.decode('utf-8')
309
+ except UnicodeDecodeError:
310
+ inp = inp.decode('iso-8859-1').encode('utf-8')
311
+ document = etree.fromstring(inp, html_parser)
315
312
  except (etree.XMLSyntaxError, UnicodeDecodeError) as e:
316
313
  # Print an error in case of syntax issues in the HTML
317
314
  print(f"Error parsing HTML: {e}", file=sys.stderr)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scrape_cli
3
- Version: 1.3.3
3
+ Version: 1.3.4
4
4
  Summary: It's a command-line tool to extract HTML elements using an XPath query or CSS3 selector.
5
5
  Author-email: Andrea Borruso <aborruso@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/aborruso/scrape-cli
@@ -1,2 +1,3 @@
1
1
  [console_scripts]
2
2
  scrape = scrape_cli.scrape:main
3
+ scrape-cli = scrape_cli.scrape:main
@@ -107,6 +107,35 @@ def test_encoding_meta_charset_iso_8859_1(tmp_path):
107
107
  assert result.stdout.strip() == "Perché"
108
108
 
109
109
 
110
+ def test_encoding_falls_back_to_iso_8859_1_without_meta(tmp_path):
111
+ html_file = tmp_path / "latin1.html"
112
+ html_file.write_bytes("<h1>Caffè</h1>".encode("iso-8859-1"))
113
+
114
+ result = run_scrape(str(html_file), "-e", "string(//h1)")
115
+
116
+ assert result.returncode == 0
117
+ assert result.stdout == "Caffè\n"
118
+
119
+
120
+ def test_encoding_utf8_without_meta_is_not_converted(tmp_path):
121
+ html_file = tmp_path / "utf8.html"
122
+ html_file.write_bytes("<h1>Caffè €</h1>".encode("utf-8"))
123
+
124
+ result = run_scrape(str(html_file), "-e", "string(//h1)")
125
+
126
+ assert result.stdout == "Caffè €\n"
127
+
128
+
129
+ def test_rawinput_keeps_the_encoding_declared_by_the_xml(tmp_path):
130
+ xml_file = tmp_path / "latin1.xml"
131
+ xml_file.write_bytes('<?xml version="1.0" encoding="iso-8859-1"?><a>Caffè</a>'.encode("iso-8859-1"))
132
+
133
+ result = run_scrape(str(xml_file), "-r", "-e", "string(//a)")
134
+
135
+ assert result.returncode == 0
136
+ assert result.stdout == "Caffè\n"
137
+
138
+
110
139
  def test_argument_extracts_attribute_value():
111
140
  result = run_scrape(str(TEST_HTML), "-e", "//a[@class='external-link']", "-a", "href")
112
141
 
File without changes
File without changes