urls and normal strings

Posted on 2005-04-07
Last Modified: 2008-01-09
Hi, i have a string which i would like to pass to an URL and get the results from the URL parsed to the resulting string.
Here is a snippet of my code

import getopt
import os
import re
import string
import sys
import urllib

### Compiled regular expressions

firstLineRE = re.compile(r'<td bgcolor=white class=s><div style=padding:10px;>(?P<mean>.*?)</div></td>')

if __name__ == "__main__":
    # process command-line args
        opts, args = getopt.getopt(sys.argv[1:], "l")
    except getopt.GetoptError:

    pico = string.replace(args[1]," ","%20")
    # Submit the query and open a file handle to the results page.
    class AppURLopener(urllib.FancyURLopener):
         def __init__(self, *args):
             self.version = "Mozilla/4.0"
             urllib.FancyURLopener.__init__(self, *args)
    urllib._urlopener = AppURLopener()
    f = urllib.urlopen("" % pico)    
    linenum = 0    
    for line in f.readlines():        

        # check this line against the appropriate RE.        
        line = line.strip()
        linenum += 1
        match =
        if match is not None:

Now the script takes a word (and some operators) and returns the spanish equivalent of it. the current problem I'm having is that it wont translate sentances or words with characters such as !@#$%^&*)(_+\ etc
anyway, I know that there are 2 problems here...first i have to convert all spaces (and other characters) to URL readable (such as spaces to %20 etc). unfortunately i dont know other than the spaces what corresponds to what, pls help.
the second problem is whenever the output as a character which cant be printed it will throw a unicode decode error...ivee been trying to catch it by putting a try statement inside the regex function but it doesnt work, please help me here as well.
thanks for your help!
Question by:Mytix
    1 Comment
    LVL 15

    Accepted Solution

    1st problem:
        pico = string.replace(args[1]," ","%20")
        pico = urllib.quote(args[1])

    2nd problem:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
    The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
    Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
    Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now