基本的腳本語言是Python,雖然不敢說是最好的語言,至少是最好的之一(0.0),用模擬登錄,咱們須要用到多個模塊,以下:html
requestsgit
BeautifulSoupgithub
下載源碼安裝cookie
git clone git://github.com/kennethreitz/requests.git cd requests pip install .
pip工具
pip install requests
Beautiful Soup 是一個能夠從HTML或XML文件中提取數據的Python庫.它可以經過你喜歡的轉換器實現慣用的文檔導航,查找,修改文檔的方式.Beautiful Soup會幫你節省數小時甚至數天的工做時間.post
easy_install beautifulsoup4
pip install beautifulsoup4
from bs4 import BeautifulSoup soup = BeautifulSoup(open("index.html")) soup = BeautifulSoup("<html>data</html>", 'lxml')
requests主要是爲了利用requests的高級會話機制,requests的會話對象能夠讓咱們跨請求保持某些參數,好比cookies, headers等,性能
會話對象讓你可以跨請求保持某些參數。它也會在同一個 Session 實例發出的全部請求之間保持 cookie, 期間使用 urllib3 的 connection pooling 功能。因此若是你向同一主機發送多個請求,底層的 TCP 鏈接將會被重用,從而帶來顯著的性能提高。url
而BeautifulSoup主要是方便解析HTML源碼,從中獲取請求須要的一些參數.net
# -*- coding: UTF-8 -*- from bs4 import BeautifulSoup import requests s = requests.Session() class CSDN: def __init__(self, username, password): self.username = username self.password = password self.login_url = 'https://passport.csdn.net/account/login' self.headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebK' 'it/537.36 (KHTML, like Gecko) Chrome/61.0.3163.1' '00 Safari/537.36 OPR/48.0.2685.52', 'Referer': 'http://my.csdn.net/my/mycsdn' } def login(self): params = { 'from': 'http://my.csdn.net/my/mycsdn' } html = s.get(self.login_url, params=params, headers=self.headers) soup = BeautifulSoup(html.content, 'lxml') lt = soup.select('input[name="lt"]')[0].get('value') execution = soup.select('input[name="execution"]')[0].get('value') event_id = soup.select('input[name="_eventId"]')[0].get('value') data = { 'username': self.username, 'password': self.password, 'rememberMe': 'true', 'lt': lt, 'execution': execution, '_eventId': event_id } r = s.post(self.login_url, data=data) self.headers['Referer'] = 'http://passport.csdn.net/account/login?from=http%3A%2F%2Fmy.csdn.net%2Fmy%2Fmycsdn' resp = s.get('http://my.csdn.net/my/mycsdn', headers=self.headers) print(resp.text) username = input('請輸入帳號:') password = input('請輸入密碼:') cs = CSDN(username, password) cs.login()
項目地址: 模擬京東登陸code
吐槽QQ羣: 173318043