分享一個(gè)常用的Python模擬登陸類
代碼非常簡(jiǎn)單,而且注釋也很詳細(xì),這里就不多廢話了
tools.py
# -*- coding:utf8 -*-
'''
# =============================================================================
# FileName: tools.py
# Desc: 模擬瀏覽器
# Author: cosven
# Email: yinshaowen241@gmail.com
# HomePage: www.cosven.com
# Version: 0.0.1
# LastChange: 2015-03-27 00:59:24
# History:
# =============================================================================
'''
import urllib
import urllib2
import cookielib
class MyWeb():
"""
模擬一個(gè)瀏覽器
"""
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):
"""
模擬post請(qǐng)求
:param string posturl: url地址
:param dict dictdata: 發(fā)送的數(shù)據(jù)
"""
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 None
def get(self, url):
"""
模擬get請(qǐng)求
:param url: url地址
:return content: 常使用read的方法來讀取返回?cái)?shù)據(jù)
:rtype : instance or None
"""
request = urllib2.Request(url, None, self.header)
try:
content = urllib2.urlopen(request)
return content
except Exception, e:
print ("open:" + str(e))
return None
if __name__ == "__main__":
import hashlib
web = MyWeb()
url = 'http://music.163.com/api/login/'
data = {
'username': 'username', # email
'password': hashlib.md5('password').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()
# 完了可以試著查看自己網(wǎng)易云音樂相應(yīng)列表歌曲
以上就是本文給大家分享的代碼了,希望大家能夠喜歡,也希望能夠?qū)Υ蠹覍W(xué)習(xí)Python有所幫助。
- python爬蟲之模擬登陸csdn的實(shí)例代碼
- python編程使用selenium模擬登陸淘寶實(shí)例代碼
- 利用selenium 3.7和python3添加cookie模擬登陸的實(shí)現(xiàn)
- Python爬蟲利用cookie實(shí)現(xiàn)模擬登陸實(shí)例詳解
- Python模擬登陸淘寶并統(tǒng)計(jì)淘寶消費(fèi)情況的代碼實(shí)例分享
- Python使用Srapy框架爬蟲模擬登陸并抓取知乎內(nèi)容
- python模擬登陸阿里媽媽生成商品推廣鏈接
- python3.3教程之模擬百度登陸代碼分享
- python模擬新浪微博登陸功能(新浪微博爬蟲)
- 詳解python項(xiàng)目實(shí)戰(zhàn):模擬登陸CSDN
相關(guān)文章
Python實(shí)現(xiàn)手繪圖效果實(shí)例分享
在本篇文章里小編給大家整理了關(guān)于Python實(shí)現(xiàn)手繪圖效果,有需要的朋友們可以學(xué)習(xí)下。2020-07-07
對(duì)Python 窗體(tkinter)樹狀數(shù)據(jù)(Treeview)詳解
今天小編就為大家分享一篇對(duì)Python 窗體(tkinter)樹狀數(shù)據(jù)(Treeview)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python獲取當(dāng)前路徑實(shí)現(xiàn)代碼
這篇文章主要介紹了 Python獲取當(dāng)前路徑實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
Python+tkinter實(shí)現(xiàn)一個(gè)繪圖風(fēng)格控件
這篇文章主要為大家詳細(xì)介紹了Python如何利用tkinter實(shí)現(xiàn)一個(gè)簡(jiǎn)單的繪圖風(fēng)格控件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2023-09-09
Python中使用語句導(dǎo)入模塊或包的機(jī)制研究
這篇文章主要介紹了Python中使用語句導(dǎo)入模塊或包的機(jī)制研究,同時(shí)對(duì)比了幾種導(dǎo)入包或模塊的語句并簡(jiǎn)要說明了這幾種方法之間的幾點(diǎn)優(yōu)劣,需要的朋友可以參考下2015-03-03
Django為窗體加上防機(jī)器人的驗(yàn)證碼功能過程解析
這篇文章主要介紹了Django為窗體加上防機(jī)器人的驗(yàn)證碼功能過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08

