環境:Ubuntu 16.04python
python版本3.5+post
import requests, lxml from bs4 import BeautifulSoup from io import BytesIO from PIL import Image class SimulationLogging: # 構造data def structure_data(self): data = { 'remember': 'on', 'backURL': 'http://weibo.cn/1786213845/fans?vt=4', 'backTitle': '微博', 'tryCount': '', 'submit': '登陸' } url = 'http://weibo.cn/1786213845/fans?vt=4' i = requests.get(url).text r = BeautifulSoup(i, "lxml") url_2 = 'http://login.weibo.cn/login/' url_login = url_2 + str(r.find('form', method="post").get('action')) password_name = r.find('input', type="password").get('name') username = input('請輸入用戶名:') data['mobile'] = username password = input('請輸入密碼') data[password_name] = password vks = r.find_all('input') data['vk'] = vks[7].get('value') data['capId'] = vks[8].get('value') img = r.find('img', alt="請打開圖片顯示").get('src') file = BytesIO(requests.get(img).content) img = Image.open(file) img.show() code = input('請輸入顯示的驗證碼(不分大小寫)') data['code'] = code img.close() self.get_content(url_login, data) # 獲取網頁內容 def get_content(self, url_login, data): request = requests.post(url_login, data=data).text r = BeautifulSoup(request, "lxml") print(r) if __name__ == '__main__': test = SimulationLogging() test.structure_data()