Sina api calls short address translation of a test

import base64
import requests


url="http://www.~~~~.com"

headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) \
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
}
params={}

def convert_to_bs64(url):
bs64_url=base64.b64encode(url.encode("utf-8"))
bs64_url=str(bs64_url,"utf-8")
#print(bs64_url)
return bs64_url 

DEF get_short_url ():
 # is obtained using the conversion function required Cookies 
R & lt requests.get = (URL = " http://dwz.wailian.work/ " , headers = headers) 
Cookies = requests.utils.dict_from_cookiejar (R & lt. Cookies) 

# call api that returns 
bs64_url = convert_to_bs64 (URL) 
params.update ({ " from " : " W " , " URL " : bs64_url, " Site " : " 1t.click " }) 
RES = Requests.get(url="http://dwz.wailian.work/api.php",\
headers=headers,cookies=cookies,params=params)
#从返回中提取短地址
print(res.text)

def main():
get_short_url()

if __name__=="__main__":
main()

#>>str={"result":"ok","data":{"short_url":"http:\/\/1t.click\/s6v","title":"\u641c\u72d7\u5ba0\u7269\u5929\u4e0b - \u7231\u5ba0\u7269,\u7231\u751f\u6d3b"}}

The above code is the short turn long, short rotation is the same length, at least, in the presence of cookies, the browser can return is input http://dwz.wailian.work/api.php?url=aHR0cDovL3QuY24vRTlFV3J1SA==&action=restore result

ps: This really took me some time yet. Use requests function, parameter headers to fill cookies, the cookies with a former page of cookies also can (I think that if the first check the login status have cookies, I did not expect ...)
Another is parmas dictionary, to set a url key - after the value that is converted from the original url to query over, I thought it was a md5, md5 encryption and decryption of a wave of research and found that a base64

In addition, the Internet has long address forwarding on Sina short address algorithm, and then probably some number crunching after md5, but the algorithm is how come, do not understand. From short address into long address, push on with inverse algorithm is almost impossible

Guess you like

Origin www.cnblogs.com/vocus/p/11404199.html