gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:28:09 24 4
gpt4 key购买 nike

我只是使用 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.unquoteurllib.unquote_plus解码 % 编码的字符串:

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

关于python - 如何使用python将编译后的url转换为normail格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32196101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com