用Python解码google的结果

2024-09-28 05:18:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着制作程序从谷歌获取网址

但问题是我有编码的网址!这样地!在

`[u'http://www.motorrad-live.de/test.php%3Fid%3D11', u'http://www.autogaleria.pl/
auto_test/test.php%3Fid%3D37', u'http://oculus.ru/test.php%3Fid%3D2', u'http://o
culus.ru/test.php%3Fid%3D1', u'http://www.kerrytaylorauctions.com/detail-test.ph
p%3Fid%3D3432', u'http://radio.ghanaweb.com/live-radio.test.php?id=3D4', u'http:
//www.studygerman.ru/test/test.php%3Fid%3D261', u'http://www.mhealth.ru/tests/te
st.php%3Fid%3D300']

正如您在.php之后看到的,有一些编码的东西!在

这里是我的代码,连我的代码内容部分都要解码!!在

^{pr2}$

Tags: 代码test程序comlivehttp编码www
2条回答

首先,需要在插值查询字符串之前引用它:

>>> urllib.quote("inurl:\"test.php?id\"")
'inurl%3A%22test.php%3Fid%22'

>>> "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&gl=de&q=%(q)s&rsz=8&start=0" % dict(q=urllib.quote("inurl:\"test.php?id\""))
'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&gl=de&q=inurl%3A%22test.php%3Fid%22&rsz=8&start=0'

其次,我查看了返回的json,发现未编码的url存储在键unescapedUrl下,因此您可以将print_results(results)替换为:

^{pr2}$

如果确实需要从url键读取,请使用:

def print_results(results):
    L=list(urllib.unquote(r['url']) for r in results)
    print L

您正在搜索unquote函数:

urllib.unquote(url)

相关问题 更多 >

    热门问题