路飛學城—python爬蟲實戰密訓-—第1章(作業)
阿新 • • 發佈:2018-07-07
找不到 sla 進行 lse sig -a 後來 spa fin
一: 學習心得,體會
- 感覺跟著視頻做的汽車之家,和抽屜網站登錄的爬蟲沒有那麽難。但做github登錄並獲取信息的,就覺得不太容易了,登錄操作挺簡單,和抽屜例子差不多,但獲取個人信息部分就感覺有點麻煩了,主要是個人信息部分在哪裏找不到,後來還是看的群裏老師分享的優秀作業,需要在登錄後獲取名字,拼接網址,轉換到個人主頁部分,再來獲取個人信息
二:
作業題目: 爬蟲實戰密訓營作業(一) 作業需求: 1. 基於requests實現自動登錄GitHub並獲取個人信息
import requests from bs4 import BeautifulSoup #1獲取token和cookie r1= requests.get( url = "https://github.com/login" ) s1 = BeautifulSoup(r1.text,‘html.parser‘) token = s1.find(name=‘input‘,attrs = {‘name‘:‘authenticity_token‘}).get(‘value‘) cookie1 = r1.cookies.get_dict() #2 進行登錄並返回登錄信息並獲取用戶名 r2 = requests.post( url="https://github.com/session", data = {‘commit‘:‘Sign in‘, ‘utf8‘:‘?‘, ‘authenticity_token‘:token, ‘login‘:‘[email protected]‘, ‘password‘:‘qq183443595‘ }, cookies=cookie1 ) s2 = BeautifulSoup(r2.text, ‘html.parser‘) #獲取登錄錯誤信息 error_text = s2.find(name = ‘div‘,attrs={‘class‘:‘flash flash-full flash-error‘}) # 賬號密碼錯誤時返回錯誤信息,登錄成功進行提示 if error_text: print(error_text.text.strip()) else: print ("登錄成功!!") # 這一步獲取用戶名之後才能有第三步,在個人主頁上獲取個人信息 name = s2.find(name=‘meta‘, attrs={‘name‘: "octolytics-actor-login"}).get(‘content‘) # 3 獲取用戶信息 r3 = requests.get( url=‘https://github.com/%s‘ % name, cookies=cookie1 ) s3 = BeautifulSoup(r3.text, ‘html.parser‘) #獲取用戶的name,nickname,repositories,activity p_name = s3.find(name=‘span‘, attrs={‘class‘: ‘p-name‘}) p_nickname = s3.find(name=‘span‘, attrs={‘class‘: ‘p-nickname‘}) p_note = s3.find(name=‘div‘, attrs={‘class‘: ‘p-note user-profile-bio‘}) repositories = s3.find(name = ‘div‘,attrs={‘class‘:‘blankslate mb-4‘}) activity = s3.find(name=‘div‘, attrs={‘class‘: ‘text-center text-gray pt-3‘}) #進行輸出 print("p_nickname:"+p_nickname.text) print ("p_name: "+p_name.text) print ("p_note: "+p_note.text) print ("repositories: "+repositories.text.strip()) print("activity: "+activity.text.strip())
路飛學城—python爬蟲實戰密訓-—第1章(作業)