By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,400 Members | 903 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,400 IT Pros & Developers. It's quick & easy.

Python urlparse how to edit parameter values?

P: 2
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:

Expand|Select|Wrap|Line Numbers
  1. from urlparse import urlparse
  2. urls = open("/path/to/urls.txt", "r").readlines()
  3. for url in urls:
  4.     addr = urlparse(url)
  5.  
addr then contains something like this:

Expand|Select|Wrap|Line Numbers
  1. ParseResult(scheme='http', netloc='www.example.com', path='/path/to/file.php', params='', query='param1=value1&param2=value2&parm3=value3', fragment='')
  2.  
addr.query contains this:
Expand|Select|Wrap|Line Numbers
  1. param1=value1&param2=value2&parm3=value3
  2.  
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.
Oct 28 '11 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Could you provide a sample of the URLs you are trying to parse and what the modified URLs are supposed to look like?
Oct 28 '11 #2

Expert 100+
P: 622
You may be able to split on the "&, change the string, and "&".join() etc.
Oct 30 '11 #3

Expert 100+
P: 391
Regular expressions would be a good way to do it.
Oct 31 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.