Skip test_skip_bad_links based on the w3lib version

This commit is contained in:
Adrián Chaves 2023-04-12 09:28:28 +02:00
parent 618e82dbe1
commit 3f0c2fae5e
3 changed files with 15 additions and 8 deletions

View File

@ -1,8 +1,8 @@
"""
Link extractor based on lxml.html
"""
import operator
import logging
import operator
from functools import partial
from urllib.parse import urljoin, urlparse
@ -94,8 +94,8 @@ class LxmlParserLinkExtractor:
try:
url = safe_url_string(url, encoding=response_encoding)
except ValueError:
logger.debug(f"Skipping extraction of bad link {url}")
continue # Disregard badly formatted urls
logger.debug(f"Skipping extraction of link with bad URL {url!r}")
continue
# to fix relative links after process_value
url = urljoin(response_url, url)

View File

@ -1,7 +1,10 @@
import pickle
import re
import unittest
import sys
from packaging.version import Version
from pytest import mark
from w3lib import __version__ as w3lib_version
from scrapy.http import HtmlResponse, XmlResponse
from scrapy.link import Link
@ -817,13 +820,16 @@ class LxmlLinkExtractorTestCase(Base.LinkExtractorTestCase):
def test_restrict_xpaths_with_html_entities(self):
super().test_restrict_xpaths_with_html_entities()
@unittest.skipIf(
sys.version_info < (3, 8),
reason="some library for python 3.7 so is less strict so bad links like htis don't crash scrapy",
@mark.skipif(
Version(w3lib_version) < Version("2.0.0"),
reason=(
"Before w3lib 2.0.0, w3lib.url.safe_url_string would not complain "
"about an invalid port value."
),
)
def test_skip_bad_links(self):
html = b"""
<a href="http://Some wierd html : http://example.com/like_this">Why would you do this?</a>
<a href="http://example.org:non-port">Why would you do this?</a>
<a href="http://example.org/item2.html">Good Link</a>
<a href="http://example.org/item3.html">Good Link 2</a>
"""

View File

@ -101,6 +101,7 @@ install_command =
python -I -m pip install {opts} {packages}
[testenv:pinned]
basepython = python3.7
deps =
{[pinned]deps}
PyDispatcher==2.0.5