分享一個(gè)常用的Python模擬登陸類
更新時(shí)間:2015年03月29日 11:25:01 作者:Cosven
這篇文章主要分享的是一個(gè)常用的Python模擬登陸類,而且可以支持cookie保存,十分的實(shí)用,這里推薦給大家,有需要的小伙伴可以參考下。
代碼非常簡單,而且注釋也很詳細(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請求
: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請求
: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用zip函數(shù)同時(shí)遍歷多個(gè)迭代器示例詳解
這篇文章主要給大家進(jìn)行介紹了Python如何用zip函數(shù)同時(shí)遍歷多個(gè)迭代器,文中給出了示例以及原理和注意事項(xiàng),相信會對大家的理解和學(xué)習(xí)很有幫助,有需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11
python sitk.show()與imageJ結(jié)合使用常見的問題
這篇文章主要介紹了python sitk.show()與imageJ結(jié)合使用常見的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
使用Python代碼實(shí)現(xiàn)對Excel單元格的鎖定
在Excel表格中,我們可以通過鎖定特定的單元格或區(qū)域,防止對單元格內(nèi)容進(jìn)行隨意修改,確保關(guān)鍵數(shù)據(jù)、公式或格式不被誤改,本文將介紹如何使用Python代碼來實(shí)現(xiàn)對Excel單元格的鎖定,實(shí)現(xiàn)批量操作以及自動化,需要的朋友可以參考下2024-06-06
ansible動態(tài)Inventory主機(jī)清單配置遇到的坑
這篇文章主要介紹了ansible動態(tài)Inventory主機(jī)清單配置遇到的坑,需要的朋友可以參考下2020-01-01
Django Channel實(shí)時(shí)推送與聊天的示例代碼
這篇文章主要介紹了Django Channel實(shí)時(shí)推送與聊天的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python游戲開發(fā)之魔塔小游戲的實(shí)現(xiàn)
魔塔小游戲作為一款角色扮演RPG小游戲,一直深受大家的喜愛。本文將利用Python的cpgames模塊制作這一經(jīng)典小游戲,感興趣的可以跟隨小編一起動手試一試2022-02-02
PyQt5?python?數(shù)據(jù)庫?表格動態(tài)增刪改詳情
這篇文章主要介紹了PyQt5?python?數(shù)據(jù)庫?表格動態(tài)增刪改詳情,首先手動連接數(shù)據(jù)庫與下一個(gè)的程序連接數(shù)據(jù)庫是獨(dú)立的2個(gè)部分,下面來看看文章的詳細(xì)介紹2022-01-01

