The code is very simple, and the annotation is very detailed, there is no more nonsense
tools.py
#-*-Coding:utf8-*-' # ============================================================================= # filename:t ools.py # Desc: Analog Browser # author:cosven # email:yinshaowen241@gmail.com # HomePage:www.cosven.com # Versi on:0.0.1 # lastchange:2015-03-27 00:59:24 # History: # ============================================================= ================ ' Import urllib import urllib2 Import Cookielib class MyWeb (): "" Simulate a Browser "" "Def __ Init__ (self): Self.header = {' Host ': ' music.163.com ', ' content-type ': ' application/x-www-form-urlencoded; Charset=utf-8 ", ' Referer ': ' http://music.163.com/song?id=26599525 '," user-agent ":" opera/8.0 "(Macintosh; PPC Mac OS X; U EN) "} Self.cookie = Cookielib. Lwpcookiejar () Self.cookie_support = Urllib2. Httpcookieprocessor (self.cookie) Self.opener = Urllib2.build_opener (Self.cookie_support, URLLIB2 . HttpHandler) Urllib2.install_opener (self. opener) def post (self, PostURL, dictdata): "" "simulate Post request:p Aram string Posturl:url address:p Aram Dict di Ctdata: Sent Data "" "PostData = Urllib.urlencode (dictdata) request = Urllib2. Request (PostURL, PostData, self.header) try:content = Urllib2.urlopen (Request) return content except Exception, E:print ("Post:" + str (e)) return the None def get (self, URL): "" "Simulate GET request:p Aram ur L:url Address: Return content: Often read the returned data using the Read method: Rtype:instance or None "" "Request = Urllib2. Request (URL, None, self.header) try:content = Urllib2.urlopen (Request) return content except Exceptio N, E:print ("Open:" + str (e)) return the None if __name__ = "__main__": Import hashlib web = MyWeb () u RL = ' http://music.163.com/api/login/' data = {' username ': ' username ', # email ' password ': hashlib.md5 (' Passwor d '). Hexdigest (), # password ' rememberlogin ': ' true '} res = web.Post (URL, data) print Res.read () # url_add = ' http://music.163.com/api/playlist/manipulate/tracks ' # data_add = { # ' tracks ': ' 26599525 ', # Music ID # ' PID ': ' 16199365 ', # Playlist ID # ' trackids ': ' [' 26599525 '] ', # Music ID STR # ' OP ': ' Add ' # opation #} # res_add = Web.post (Url_add, Data_add) # print res_add.read () # You can try to look at yourself
NetEase cloud music corresponding list song
The above is the article to share the code, I hope you can enjoy, but also hope to learn Python help.