tags:

views:

68

answers:

3

A url looks like:

http://www.example.com/cgi-bin/blahblah?&PC=abd23423&uy=020

I need to extract the value: abc23423

I tried this regex but its not working:

rx = re.compile(r'PC=(\w*)&uy=')

I then I did:

pc = rx.search(url).groups()

but I get an error:

attribute error: nonetype object has no attribute groups.

+4  A: 

Try urlparse.

jtbandes
i tried: urlparse(url).query and then passing that to urlparse.parse_qs but it says there is no function parse_qs do I have an old version?
Blankman
@Blankman: Which version of Python are you using?
Manoj Govindan
version 2.6.5....
Blankman
Strange. I am using v2.6.2 (on Ubuntu Jaunty) and `urlparse` has a `parse_qs` function.
Manoj Govindan
@Blankman: did you do `import urlparse` first?
jtbandes
yes, I did from module which was causing the issue. thank.s
Blankman
+2  A: 

Update

Sheesh. What was I thinking?

import urlparse
u = 'http://www.example.com/cgi-bin/blahblah?&PC=abd23423&uy=020'
query = urlparse.urlparse(u).query
urlparse.parse_qs(query) # {'PC': ['abd23423'], 'uy': ['020']}

Original Answer

This code snippet worked for me. Take a look:

import urlparse, re

u = 'http://www.example.com/cgi-bin/blahblah?&PC=abd23423&uy=020'
query = urlparse.urlparse(u).query

pattern = re.compile('PC=(\w*)&uy')
pattern.findall(query) # ['abd23423']
Manoj Govindan
A: 
lol = "http://www.example.com/cgi-bin/blahblah?&PC=abd23423&uy=020"
s = re.compile("&PC=(\w+)&uy=")
g = s.search(lol)
g.groups()
('abd23423',)

This seems to work for me.

xitrium