开发者

What can be used instead of parse_qs function

开发者 https://www.devze.com 2023-02-03 05:05 出处:网络
I have the following code for parsing youtube feed and returning youtube movie id. How can I rewrite this to be python 2.4 compatible which I suppose doesn\'t support parse_qs function ?

I have the following code for parsing youtube feed and returning youtube movie id. How can I rewrite this to be python 2.4 compatible which I suppose doesn't support parse_qs function ?

YTSearchFeed = feedparser.parse("http://gdata.youtube.com" + path)
videos = []
for yt in Y开发者_C百科TSearchFeed.entries:
    url_data = urlparse.urlparse(yt['link']) 
    query = urlparse.parse_qs(url_data[4])
    id = query["v"][0]
    videos.append(id) 


I assume your existing code runs in 2.6 or something newer, and you're trying to go back to 2.4? parse_qs used to be in the cgi module before it was moved to urlparse. Try import cgi, cgi.parse_qs.

Inspired by TryPyPy's comment, I think you could make your source run in either environment by doing:

import urlparse # if we're pre-2.6, this will not include parse_qs
try:
    from urlparse import parse_qs
except ImportError: # old version, grab it from cgi
    from cgi import parse_qs
    urlparse.parse_qs = parse_qs

But I don't have 2.4 to try this out, so no promises.


I tried that, and still.. it wasn't working.

It's easier to simply copy the parse_qs/qsl functions over from the cgi module to the urlparse module.

Problem solved.

0

精彩评论

暂无评论...
验证码 换一张
取 消