1. 程式人生 > >使用cookiejar模擬登入微博,獲取登入後的頁面資料

使用cookiejar模擬登入微博,獲取登入後的頁面資料

一、匯入模組

import urllib.request
import http.cookiejar
import urllib.parse
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

二、依次建立cookiejar物件、handler物件、opener物件

cookie = http.cookiejar.CookieJar()
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)

三、模擬登入資料

  • 獲取登入表單的提交地址
post_url = 'https://passport.weibo.cn/sso/login'
  • 查看錶單提交欄位,設計模擬資料
data = {
    'username':'18570329382',
    'password':'1126482',
    'savestate':'1',
    'r':'http://weibo.cn/',
    'ec':'0',
    'pagerefer':'',
    'entry':'mweibo',
    'wentry':'' ,
    'loginfrom':'',
    'client_id'
:'', 'code':'', 'qq':'', 'mainpageflag':'1', 'vid':'fef0ad08eb4bf0b3e04a026f8778705614f877870561', 'hff':'', 'hfp':'', } data = urllib.parse.urlencode(data).encode('utf-8')
  • 構造headers
headers = {
    'Host': 'passport.weibo.cn',
    'Connection':'keep-alive',
    'Content-Length'
:'219', 'Origin':'https://passport.weibo.cn', 'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML like Gecko) Chrome/63.0.3239.84 Safari/537.36', 'Content-Type': 'application/x-www-form-urlencoded', 'Accept':'*/*', 'Referer':'https://passport.weibo.cn/signin/login?entry=mweibo&r=http%3A%2F%2Fweibo.cn%2F&backTitle=%CE%A2%B2%A9&vt=', 'Accept-Encoding':'gzip, deflate, br', 'Accept-Language':'zh-CN,zh;q=0.9', }

四、模擬請求

request = urllib.request.Request(url=post_url,data=data,headers=headers)

五、傳送請求進行登入

response = opener.open(request)

六、用opener物件訪問登入後的詳情頁(opner物件自動儲存了登入後的cookies)

url = 'https://weibo.cn/5613030371/info'
headers1 = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML like Gecko) Chrome/63.0.3239.84 Safari/537.36',

}
request = urllib.request.Request(url=url,headers=headers1)

response1 = opener.open(request)
print(response1.read())