Scrapy, a fast high-level web crawling & scraping framework for Python.
PY3: use six for robotparser and urlparse
Felix Yan6f7efa1d1df5
diff --git a/extras/scrapy-ws.py b/extras/scrapy-ws.pyindex 60fdfd4af..b95298d6e 100755--- a/extras/scrapy-ws.py+++ b/extras/scrapy-ws.py@@ -13,7 +13,7 @@ command).from __future__ import print_functionimport sys, optparse, urllib, json-from urlparse import urljoin+from six.moves.urllib.parse import urljoinfrom scrapy.utils.jsonrpc import jsonrpc_client_call, JsonRpcErrordiff --git a/scrapy/commands/deploy.py b/scrapy/commands/deploy.pyindex 62b57d3ed..051ce661a 100644--- a/scrapy/commands/deploy.py+++ b/scrapy/commands/deploy.py@@ -8,7 +8,7 @@ import timeimport urllib2import netrcimport json-from urlparse import urlparse, urljoin+from six.moves.urllib.parse import urlparse, urljoinfrom subprocess import Popen, PIPE, check_callfrom w3lib.form import encode_multipartdiff --git a/scrapy/contrib/downloadermiddleware/httpproxy.py b/scrapy/contrib/downloadermiddleware/httpproxy.pyindex 6a6a049bf..ce09655d0 100644--- a/scrapy/contrib/downloadermiddleware/httpproxy.py+++ b/scrapy/contrib/downloadermiddleware/httpproxy.py@@ -1,7 +1,7 @@import base64from urllib import getproxies, unquote, proxy_bypassfrom urllib2 import _parse_proxy-from urlparse import urlunparse+from six.moves.urllib.parse import urlunparsefrom scrapy.utils.httpobj import urlparse_cachedfrom scrapy.exceptions import NotConfigureddiff --git a/scrapy/contrib/downloadermiddleware/redirect.py b/scrapy/contrib/downloadermiddleware/redirect.pyindex 1b36a7eae..6a42987e1 100644--- a/scrapy/contrib/downloadermiddleware/redirect.py+++ b/scrapy/contrib/downloadermiddleware/redirect.py@@ -1,4 +1,4 @@-from urlparse import urljoin+from six.moves.urllib.parse import urljoinfrom scrapy import logfrom scrapy.http import HtmlResponsediff --git a/scrapy/contrib/downloadermiddleware/robotstxt.py b/scrapy/contrib/downloadermiddleware/robotstxt.pyindex cc9f863f9..f1e8012e7 100644--- a/scrapy/contrib/downloadermiddleware/robotstxt.py+++ b/scrapy/contrib/downloadermiddleware/robotstxt.py@@ -4,7 +4,7 @@ enable this middleware and enable the ROBOTSTXT_OBEY setting."""-import robotparser+from six.moves.urllib import robotparserfrom scrapy import signals, logfrom scrapy.exceptions import NotConfigured, IgnoreRequestdiff --git a/scrapy/contrib/feedexport.py b/scrapy/contrib/feedexport.pyindex bf16cd0f4..941f9633e 100644--- a/scrapy/contrib/feedexport.py+++ b/scrapy/contrib/feedexport.py@@ -7,7 +7,7 @@ See documentation in docs/topics/feed-exports.rstimport sys, os, posixpathfrom tempfile import TemporaryFilefrom datetime import datetime-from urlparse import urlparse+from six.moves.urllib.parse import urlparsefrom ftplib import FTPfrom zope.interface import Interface, implementsdiff --git a/scrapy/contrib/linkextractors/htmlparser.py b/scrapy/contrib/linkextractors/htmlparser.pyindex 6e3c5071a..fff9eabe6 100644--- a/scrapy/contrib/linkextractors/htmlparser.py+++ b/scrapy/contrib/linkextractors/htmlparser.py@@ -3,7 +3,7 @@ HTMLParser-based link extractor"""from HTMLParser import HTMLParser-from urlparse import urljoin+from six.moves.urllib.parse import urljoinfrom w3lib.url import safe_url_stringdiff --git a/scrapy/contrib/linkextractors/lxmlhtml.py b/scrapy/contrib/linkextractors/lxmlhtml.pyindex efe24b835..b6de74f33 100644--- a/scrapy/contrib/linkextractors/lxmlhtml.py+++ b/scrapy/contrib/linkextractors/lxmlhtml.py@@ -3,7 +3,7 @@ Link extractor based on lxml.html"""import re-from urlparse import urlparse, urljoin+from six.moves.urllib.parse import urlparse, urljoinimport lxml.etree as etreediff --git a/scrapy/contrib/linkextractors/regex.py b/scrapy/contrib/linkextractors/regex.pyindex db2c9db8e..e9d77e618 100644--- a/scrapy/contrib/linkextractors/regex.py+++ b/scrapy/contrib/linkextractors/regex.py@@ -1,5 +1,5 @@import re-from urlparse import urljoin+from six.moves.urllib.parse import urljoinfrom w3lib.html import remove_tags, remove_entities, replace_escape_charsdiff --git a/scrapy/contrib/linkextractors/sgml.py b/scrapy/contrib/linkextractors/sgml.pyindex 4e54e9b7e..9a55c1581 100644--- a/scrapy/contrib/linkextractors/sgml.py+++ b/scrapy/contrib/linkextractors/sgml.py@@ -1,7 +1,7 @@"""SGMLParser-based Link extractors"""-from urlparse import urljoin+from six.moves.urllib.parse import urljoinimport warningsfrom sgmllib import SGMLParserdiff --git a/scrapy/contrib/pipeline/files.py b/scrapy/contrib/pipeline/files.pyindex 09a0ca665..8c43982a1 100644--- a/scrapy/contrib/pipeline/files.py+++ b/scrapy/contrib/pipeline/files.py@@ -7,7 +7,7 @@ import osimport os.pathimport rfc822import time-import urlparse+from six.moves.urllib.parse import urlparsefrom collections import defaultdictfrom cStringIO import StringIOimport six@@ -167,7 +167,7 @@ class FilesPipeline(MediaPipeline):if os.path.isabs(uri): # to support win32 paths like: C:\\some\dirscheme = 'file'else:- scheme = urlparse.urlparse(uri).scheme+ scheme = urlparse(uri).schemestore_cls = self.STORE_SCHEMES[scheme]return store_cls(uri)diff --git a/scrapy/core/downloader/handlers/ftp.py b/scrapy/core/downloader/handlers/ftp.pyindex 70c526490..6b5f03bda 100644--- a/scrapy/core/downloader/handlers/ftp.py+++ b/scrapy/core/downloader/handlers/ftp.py@@ -29,7 +29,7 @@ In case of status 200 request, response.headers will come with two keys:"""import re-from urlparse import urlparse+from six.moves.urllib.parse import urlparsefrom cStringIO import StringIOfrom twisted.internet import reactordiff --git a/scrapy/core/downloader/handlers/http11.py b/scrapy/core/downloader/handlers/http11.pyindex 617a68ea4..455794b14 100644--- a/scrapy/core/downloader/handlers/http11.py+++ b/scrapy/core/downloader/handlers/http11.py@@ -4,7 +4,7 @@ import refrom time import timefrom cStringIO import StringIO-from urlparse import urldefrag+from six.moves.urllib.parse import urldefragfrom zope.interface import implementsfrom twisted.internet import defer, reactor, protocoldiff --git a/scrapy/core/downloader/webclient.py b/scrapy/core/downloader/webclient.pyindex 135d8f293..2c6a61b8a 100644--- a/scrapy/core/downloader/webclient.py+++ b/scrapy/core/downloader/webclient.py@@ -1,5 +1,5 @@from time import time-from urlparse import urlparse, urlunparse, urldefrag+from six.moves.urllib.parse import urlparse, urlunparse, urldefragfrom twisted.web.client import HTTPClientFactoryfrom twisted.web.http import HTTPClientdiff --git a/scrapy/http/request/form.py b/scrapy/http/request/form.pyindex a4f45a306..53488e1a1 100644--- a/scrapy/http/request/form.py+++ b/scrapy/http/request/form.py@@ -5,7 +5,8 @@ This module implements the FormRequest class which is a more covenient classSee documentation in docs/topics/request-response.rst"""-import urllib, urlparse+import urllib+from six.moves.urllib.parse import urljoinimport lxml.htmlimport sixfrom scrapy.http.request import Request@@ -43,7 +44,7 @@ class FormRequest(Request):def _get_form_url(form, url):if url is None:return form.action or form.base_url- return urlparse.urljoin(form.base_url, url)+ return urljoin(form.base_url, url)def _urlencode(seq, enc):values = [(unicode_to_str(k, enc), unicode_to_str(v, enc))diff --git a/scrapy/linkextractor.py b/scrapy/linkextractor.pyindex d9c85b50a..5badea5e5 100644--- a/scrapy/linkextractor.py+++ b/scrapy/linkextractor.py@@ -3,7 +3,7 @@ Common code and definitions used by Link extractors (located inscrapy.contrib.linkextractor)."""import re-from urlparse import urlparse+from six.moves.urllib.parse import urlparsefrom scrapy.utils.url import url_is_from_any_domainfrom scrapy.utils.url import canonicalize_url, url_is_from_any_domain, url_has_any_extensiondiff --git a/scrapy/tests/test_contrib_feedexport.py b/scrapy/tests/test_contrib_feedexport.pyindex cf85ea1e7..7a1b3dc8f 100644--- a/scrapy/tests/test_contrib_feedexport.py+++ b/scrapy/tests/test_contrib_feedexport.py@@ -1,4 +1,5 @@-import os, urlparse+import os+from six.moves.urllib.parse import urlparsefrom cStringIO import StringIOfrom zope.interface.verify import verifyObject@@ -84,7 +85,7 @@ class S3FeedStorageTest(unittest.TestCase):file = storage.open(Spider("default"))file.write("content")yield storage.store(file)- u = urlparse.urlparse(uri)+ u = urlparse(uri)key = connect_s3().get_bucket(u.hostname, validate=False).get_key(u.path)self.failUnlessEqual(key.get_contents_as_string(), "content")diff --git a/scrapy/tests/test_engine.py b/scrapy/tests/test_engine.pyindex cabdff9bb..2881b9168 100644--- a/scrapy/tests/test_engine.py+++ b/scrapy/tests/test_engine.py@@ -11,7 +11,8 @@ module with the ``runserver`` argument::"""from __future__ import print_function-import sys, os, re, urlparse+import sys, os, re+from six.moves.urllib.parse import urlparsefrom twisted.internet import reactor, deferfrom twisted.web import server, static, util@@ -117,7 +118,7 @@ class CrawlerRun(object):return "http://localhost:%s%s" % (self.portno, path)def getpath(self, url):- u = urlparse.urlparse(url)+ u = urlparse(url)return u.pathdef item_scraped(self, item, spider, response):diff --git a/scrapy/tests/test_http_cookies.py b/scrapy/tests/test_http_cookies.pyindex bead255c5..3d6993491 100644--- a/scrapy/tests/test_http_cookies.py+++ b/scrapy/tests/test_http_cookies.py@@ -1,4 +1,4 @@-from urlparse import urlparse+from six.moves.urllib.parse import urlparsefrom unittest import TestCasefrom scrapy.http import Request, Responsediff --git a/scrapy/tests/test_http_request.py b/scrapy/tests/test_http_request.pyindex ebcee8c1d..d933df258 100644--- a/scrapy/tests/test_http_request.py+++ b/scrapy/tests/test_http_request.py@@ -1,7 +1,7 @@import cgiimport unittestimport xmlrpclib-from urlparse import urlparse+from six.moves.urllib.parse import urlparsefrom scrapy.http import Request, FormRequest, XmlRpcRequest, Headers, HtmlResponsediff --git a/scrapy/tests/test_spidermiddleware_offsite.py b/scrapy/tests/test_spidermiddleware_offsite.pyindex 7e0d248dc..31ed9c909 100644--- a/scrapy/tests/test_spidermiddleware_offsite.py+++ b/scrapy/tests/test_spidermiddleware_offsite.py@@ -5,7 +5,7 @@ from scrapy.spider import Spiderfrom scrapy.contrib.spidermiddleware.offsite import OffsiteMiddlewarefrom scrapy.utils.test import get_crawler-from urlparse import urlparse+from six.moves.urllib.parse import urlparseclass TestOffsiteMiddleware(TestCase):diff --git a/scrapy/tests/test_urlparse_monkeypatches.py b/scrapy/tests/test_urlparse_monkeypatches.pyindex 91d75789c..052dde37f 100644--- a/scrapy/tests/test_urlparse_monkeypatches.py+++ b/scrapy/tests/test_urlparse_monkeypatches.py@@ -1,4 +1,4 @@-from urlparse import urlparse+from six.moves.urllib.parse import urlparseimport unittestdiff --git a/scrapy/tests/test_utils_httpobj.py b/scrapy/tests/test_utils_httpobj.pyindex e978ae851..4f9f7a370 100644--- a/scrapy/tests/test_utils_httpobj.py+++ b/scrapy/tests/test_utils_httpobj.py@@ -1,5 +1,5 @@import unittest-import urlparse+from six.moves.urllib.parse import urlparsefrom scrapy.http import Requestfrom scrapy.utils.httpobj import urlparse_cached@@ -13,7 +13,7 @@ class HttpobjUtilsTest(unittest.TestCase):req1a = urlparse_cached(request1)req1b = urlparse_cached(request1)req2 = urlparse_cached(request2)- urlp = urlparse.urlparse(url)+ urlp = urlparse(url)assert req1a == req2assert req1a == urlpdiff --git a/scrapy/tests/test_utils_response.py b/scrapy/tests/test_utils_response.pyindex 5a646a34e..92c92c057 100644--- a/scrapy/tests/test_utils_response.py+++ b/scrapy/tests/test_utils_response.py@@ -1,6 +1,6 @@import osimport unittest-import urlparse+from six.moves.urllib.parse import urlparsefrom scrapy.http import Response, TextResponse, HtmlResponsefrom scrapy.utils.response import response_httprepr, open_in_browser, get_meta_refresh@@ -24,7 +24,7 @@ class ResponseUtilsTest(unittest.TestCase):url = "http:///www.example.com/some/page.html"body = "<html> <head> <title>test page</title> </head> <body>test body</body> </html>"def browser_open(burl):- path = urlparse.urlparse(burl).path+ path = urlparse(burl).pathif not os.path.exists(path):path = burl.replace('file://', '')bbody = open(path).read()diff --git a/scrapy/tests/test_webclient.py b/scrapy/tests/test_webclient.pyindex 4e01f121b..a16eb1ccf 100644--- a/scrapy/tests/test_webclient.py+++ b/scrapy/tests/test_webclient.py@@ -3,7 +3,7 @@ from twisted.internet import deferTests borrowed from the twisted.web.client tests."""import os-from urlparse import urlparse+from six.moves.urllib.parse import urlparsefrom twisted.trial import unittestfrom twisted.web import server, static, error, utildiff --git a/scrapy/utils/httpobj.py b/scrapy/utils/httpobj.pyindex 49d2f9d7b..b4c929b0e 100644--- a/scrapy/utils/httpobj.py+++ b/scrapy/utils/httpobj.py@@ -2,7 +2,7 @@import weakref-from urlparse import urlparse+from six.moves.urllib.parse import urlparse_urlparse_cache = weakref.WeakKeyDictionary()def urlparse_cached(request_or_response):diff --git a/scrapy/utils/request.py b/scrapy/utils/request.pyindex 3d65cb765..0e6e6675d 100644--- a/scrapy/utils/request.py+++ b/scrapy/utils/request.py@@ -6,7 +6,7 @@ scrapy.http.Request objectsfrom __future__ import print_functionimport hashlibimport weakref-from urlparse import urlunparse+from six.moves.urllib.parse import urlunparsefrom twisted.internet.defer import Deferredfrom w3lib.http import basic_auth_headerdiff --git a/scrapy/utils/testsite.py b/scrapy/utils/testsite.pyindex 1c221b23c..9f8419814 100644--- a/scrapy/utils/testsite.py+++ b/scrapy/utils/testsite.py@@ -1,5 +1,5 @@from __future__ import print_function-import urlparse+from six.moves.urllib.parse import urljoinfrom twisted.internet import reactorfrom twisted.web import server, resource, static, util@@ -14,7 +14,7 @@ class SiteTest(object):self.site.stopListening()def url(self, path):- return urlparse.urljoin(self.baseurl, path)+ return urljoin(self.baseurl, path)def test_site():r = resource.Resource()diff --git a/scrapy/utils/url.py b/scrapy/utils/url.pyindex 3c86a6edb..50107b434 100644--- a/scrapy/utils/url.py+++ b/scrapy/utils/url.py@@ -6,7 +6,7 @@ Some of the functions that used to be imported from this module have been movedto the w3lib.url module. Always import those from there instead."""import posixpath-import urlparse+from six.moves.urllib.parse import ParseResult, urlunparse, urldefrag, urlparseimport urllibimport cgi@@ -59,7 +59,7 @@ def canonicalize_url(url, keep_blank_values=True, keep_fragments=False,query = urllib.urlencode(keyvals)path = safe_url_string(_unquotepath(path)) or '/'fragment = '' if not keep_fragments else fragment- return urlparse.urlunparse((scheme, netloc.lower(), path, params, query, fragment))+ return urlunparse((scheme, netloc.lower(), path, params, query, fragment))def _unquotepath(path):@@ -72,8 +72,8 @@ def parse_url(url, encoding=None):"""Return urlparsed url from the given argument (which could be an alreadyparsed url)"""- return url if isinstance(url, urlparse.ParseResult) else \- urlparse.urlparse(unicode_to_str(url, encoding))+ return url if isinstance(url, ParseResult) else \+ urlparse(unicode_to_str(url, encoding))def escape_ajax(url):@@ -99,7 +99,7 @@ def escape_ajax(url):>>> escape_ajax("www.example.com/ajax.html")'www.example.com/ajax.html'"""- defrag, frag = urlparse.urldefrag(url)+ defrag, frag = urldefrag(url)if not frag.startswith('!'):return urlreturn add_or_replace_parameter(defrag, '_escaped_fragment_', frag[1:])