Python urlparse how to edit parameter values?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madlax
    New Member
    • Oct 2011
    • 2

    Python urlparse how to edit parameter values?

    I have a long list of URL's that I need my Python script to iterate through. I need it to replace the parameter values in the URL with a certain string, and keep the parameter names the same.
    But I have encounted a bit of a problem:

    Code:
    from urlparse import urlparse
    urls = open("/path/to/urls.txt", "r").readlines()
    for url in urls:
        addr = urlparse(url)
    addr then contains something like this:

    Code:
    ParseResult(scheme='http', netloc='www.example.com', path='/path/to/file.php', params='', query='param1=value1&param2=value2&parm3=value3', fragment='')
    addr.query contains this:
    Code:
    param1=value1&param2=value2&parm3=value3
    I would like to replace all the parameter values ( in this case value1, value2 and value3) with a certain string, the same string for each of them.

    The problem is urlparse squishes both the parameter names and thier values into the same element in a list, so its not so easy to automate replacing the parameter values with a string, while keeping the names the same. Anyone know how I can do this? I would appreciate any help on this, thank you.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Could you provide a sample of the URLs you are trying to parse and what the modified URLs are supposed to look like?

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      You may be able to split on the "&, change the string, and "&".join() etc.

      Comment

      • Glenton
        Recognized Expert Contributor
        • Nov 2008
        • 391

        #4
        Regular expressions would be a good way to do it.

        Comment

        Working...