cut strings and parse for images

Paul McGuire ptmcg at austin.rr._bogus_.com
Mon Dec 6 19:40:02 EST 2004


"Andreas Volz" <usenet-spam-trap at brachttal.net> wrote in message
news:20041206234141.3787521b at frodo.mittelerde...
> Am Mon, 06 Dec 2004 20:36:36 GMT schrieb Paul McGuire:
>
> > Check out the urlparse module (in std distribution).  For images, you
> > can provide a default addressing scheme, so you can expand
> > "images/marine.jpg" relative to the current location.
>
> Ok, this looks good. But I'm a really newbie to python and not able to
> create a minimum example. Could you please give me a very small example
> how to use urlparse? Or point me to an example in the web?
>
> regards
> Andreas

No problem.  Googling for 'python urlparse' gets us immediately to:
http://www.python.org/doc/current/lib/module-urlparse.html.  This online doc
has some examples built into it.

But as a newbie, it would also be good to get comfortable with dir() and
help() and trying simple commands at the >>> Python prompt.  If I type the
following at the Python prompt:
>>> import urlparse
>>> help(urlparse)
I get almost the same output straight from the Python source.

dir(urlparse) gives me just a list of the global symbol names from the
module, but sometimes that's enough of a clue without reading the whole doc.

Now is where the intrepid Pythonista-to-be uses the Python interactive
prompt and the tried-and-true Python methodology known as "Just Trying Stuff
Out".

>>> url = "http://www.example.com/dir/example.html"        # from your
example
>>> urlparse(url)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'module' object is not callable
(Damn! forgot to prefix with urlparse.)
>>> urlparse.urlparse(url)
('http', 'www.example.com', '/dir/example.html', '', '', '')
>>> img = "images/marine.jpeg"   # also from your example
>>> urlparse.urlparse(img)
('', '', 'images/marine.jpeg', '', '', '')

Now you can start to predict what kind of tuples you'll get back from
urlparse, you can visualize how you might merge the data from the img
fragment and the url fragment.  Wait, I didn't read all of the doc - let's
try urljoin!
>>> urljoin(url,img)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'urljoin' is not defined
(Damn! forgot to prefix with urlparse AGAIN!)
>>> urlparse.urljoin(url,img)
'http://www.example.com/dir/images/marine.jpeg'

Is this in the ballpark of where you are trying to go?

-- Paul
Give a man a fish and you feed him for a day; give a man a fish every day
and you feed him for the rest of his life.





More information about the Python-list mailing list