1. 程式人生 > 實用技巧 >獲取人人網當前使用者的個人詳情頁資料

獲取人人網當前使用者的個人詳情頁資料

#編碼流程:
#1.驗證碼的識別,獲取驗證碼圖片的文字資料
#2.對post請求進行傳送(處理請求函式)
#3.對響應函式進行持久化儲存

import requests
from lxml import etree
from CodeClass import YDMHttp

#建立一個session物件
session = requests.Session()

#1.對驗證碼圖片進行捕獲和識別
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2626.106 Safari/537.36'
}

url = 'http://www.renren.com/SysHome.do'
page_text = requests.get(url=url,headers=headers).text

tree = etree.HTML(page_text)
code_img_src = etree.xpath('//*[@id="verifyPic_login"]/@src')[0]
code_img_data = requests.get(url=code_img_src,headers=headers).content
with open('./code.jpg','wb') as fp:
    fp.write(code_img_data)

#使用雲打碼提供的示例程式碼對驗證碼圖片進行識別
result = getCodeText('code.jpg',2004)

#post請求的傳送(模擬登入)
login_url = ''

data = {

}

#使用session進行post請求的傳送
response = session.post(url=login_url,headers=headers,data=data)
print(response.status_code)

#爬取當前使用者個人主頁對應的頁面資料
detail_url = 'http://www.renren.com/974813621/profile'
# headers = {
#     'Cookie':'xxxx'
# }
#使用攜帶cookie的session進行get請求傳送
detail_page_test = session.get(url=detail_url,headers=headers).text
with open('./bob.html','w',encoding='utf-8') as fp:
    fp.write(detail_page_text)