Commits

Gael Pasgrimaud committed dbbfaaa

add opener argument

  • Participants
  • Parent commits b05c03b

Comments (0)

Files changed (3)

File pyquery/README.txt

 
     >>> from pyquery import PyQuery as pq
     >>> from lxml import etree
+    >>> import urllib
     >>> d = pq("<html></html>")
     >>> d = pq(etree.fromstring("<html></html>"))
     >>> d = pq(url='http://google.com/')
+    >>> d = pq(url='http://google.com/', opener=lambda url: urllib.urlopen(url).read())
     >>> d = pq(filename=path_to_html_file)
 
 Now d is like the $ in jquery::

File pyquery/pyquery.py

             if 'filename' in kwargs:
                 html = file(kwargs['filename']).read()
             elif 'url' in kwargs:
-                from urllib2 import urlopen
-                url = kwargs['url']
-                html = urlopen(url).read()
+                url = kwargs.pop('url')
+                if 'opener' in kwargs:
+                    opener = kwargs.pop('opener')
+                    html = opener(url)
+                else:
+                    from urllib2 import urlopen
+                    html = urlopen(url).read()
                 self._base_url = url
             else:
                 raise ValueError('Invalid keyword arguments %s' % kwargs)

File pyquery/test.py

         assert len(self.klass('div', self.html).find('span').end()) == 2
         assert len(self.klass('#node2', self.html).find('span').end()) == 1
 
+
+class TestOpener(unittest.TestCase):
+
+    def test_custom_opener(self):
+        def opener(url):
+            return '<html><body><div class="node"></div>'
+
+        doc = pq(url='http://example.com', opener=opener)
+        assert len(doc('.node')) == 1, doc
+
 def application(environ, start_response):
     req = Request(environ)
     response = Response()