简体   繁体   中英

Grab product code from a url, do I need regex for this?

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.

尝试urlparse

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']
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM