Skip to main content
 首页 » 编程设计

python之如何使用python将编译后的url转换为normail格式

2024年10月24日21xing901022

我只是使用 gogole api 通过 python 进行搜索并使用下面的脚本

import urllib 
import json as m_json 
query = raw_input ( 'Query: ' ) 
query = urllib.urlencode ( { 'q' : query } ) 
response = urllib.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query ).read() 
json = m_json.loads ( response ) 
results = json [ 'responseData' ] [ 'results' ] 
for result in results: 
 
    url = result['url']   # was URL in the original and that threw a name error exception 
    print (  url ) 

然后我得到以下结果:

Query: inurl:"readnews.php?id=" 
http://www.idmantv.az/readnews.php%3Fid%3D14999 
http://www.kanda.com/readnews.php%3Fid%3D9 
http://www.dcfever.com/news/readnews.php%3Fid%3D12573 
http://www.thegrower.org/readnews.php%3Fid%3D6c0p5n0e8i6b 

但我想要这个 url 的正常形式,例如

http://www.idmantv.az/readnews.php?id=14999 

如何用 python 做到这一点?

请您参考如下方法:

使用urllib.unquote或 urllib.unquote_plus解码 % 编码的字符串:

>>> urllib.unquote('http://www.idmantv.az/readnews.php%3Fid%3D14999') 
'http://www.idmantv.az/readnews.php?id=14999'