1. 程式人生 > >基於Requests和BeautifulSoup實現“自動登錄”

基於Requests和BeautifulSoup實現“自動登錄”

mem input set 用戶驗證 bin spa ida password live

基於Requests和BeautifulSoup實現“自動登錄”實例

自動登錄抽屜新熱榜

技術分享圖片
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests


# ############## 方式一 ##############
"""
# ## 1、首先登陸任何頁面,獲取cookie
i1 = requests.get(url="http://dig.chouti.com/help/service")
i1_cookies = i1.cookies.get_dict()

# ## 2、用戶登陸,攜帶上一次的cookie,後臺對cookie中的 gpsd 進行授權
i2 = requests.post(
    url="http://dig.chouti.com/login",
    data={
        ‘phone‘: "8615131255089",
        ‘password‘: "xxooxxoo",
        ‘oneMonth‘: ""
    },
    cookies=i1_cookies
)

# ## 3、點贊(只需要攜帶已經被授權的gpsd即可)
gpsd = i1_cookies[‘gpsd‘]
i3 = requests.post(
    url="http://dig.chouti.com/link/vote?linksId=8589523",
    cookies={‘gpsd‘: gpsd}
)

print(i3.text)
""" # ############## 方式二 ############## """ import requests session = requests.Session() i1 = session.get(url="http://dig.chouti.com/help/service") i2 = session.post( url="http://dig.chouti.com/login", data={ ‘phone‘: "8615131255089", ‘password‘: "xxooxxoo", ‘oneMonth‘: "" } ) i3 = session.post( url="http://dig.chouti.com/link/vote?linksId=8589523" ) print(i3.text)
""" 抽屜新熱榜
示例

自動登錄GitHub

技術分享圖片
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import requests
from bs4 import BeautifulSoup

# ############## 方式一 ##############
#
# # 1. 訪問登陸頁面,獲取 authenticity_token
# i1 = requests.get(‘https://github.com/login‘)
# soup1 = BeautifulSoup(i1.text, features=‘lxml‘)
# tag = soup1.find(name=‘input‘, attrs={‘name‘: ‘authenticity_token‘}) # authenticity_token = tag.get(‘value‘) # c1 = i1.cookies.get_dict() # i1.close() # # # 1. 攜帶authenticity_token和用戶名密碼等信息,發送用戶驗證 # form_data = { # "authenticity_token": authenticity_token, # "utf8": "", # "commit": "Sign in", # "login": "[email protected]", # ‘password‘: ‘xxoo‘ # } # # i2 = requests.post(‘https://github.com/session‘, data=form_data, cookies=c1) # c2 = i2.cookies.get_dict() # c1.update(c2) # i3 = requests.get(‘https://github.com/settings/repositories‘, cookies=c1) # # soup3 = BeautifulSoup(i3.text, features=‘lxml‘) # list_group = soup3.find(name=‘div‘, class_=‘listgroup‘) # # from bs4.element import Tag # # for child in list_group.children: # if isinstance(child, Tag): # project_tag = child.find(name=‘a‘, class_=‘mr-1‘) # size_tag = child.find(name=‘small‘) # temp = "項目:%s(%s); 項目路徑:%s" % (project_tag.get(‘href‘), size_tag.string, project_tag.string, ) # print(temp) # ############## 方式二 ############## # session = requests.Session() # # 1. 訪問登陸頁面,獲取 authenticity_token # i1 = session.get(‘https://github.com/login‘) # soup1 = BeautifulSoup(i1.text, features=‘lxml‘) # tag = soup1.find(name=‘input‘, attrs={‘name‘: ‘authenticity_token‘}) # authenticity_token = tag.get(‘value‘) # c1 = i1.cookies.get_dict() # i1.close() # # # 1. 攜帶authenticity_token和用戶名密碼等信息,發送用戶驗證 # form_data = { # "authenticity_token": authenticity_token, # "utf8": "", # "commit": "Sign in", # "login": "[email protected]", # ‘password‘: ‘xxoo‘ # } # # i2 = session.post(‘https://github.com/session‘, data=form_data) # c2 = i2.cookies.get_dict() # c1.update(c2) # i3 = session.get(‘https://github.com/settings/repositories‘) # # soup3 = BeautifulSoup(i3.text, features=‘lxml‘) # list_group = soup3.find(name=‘div‘, class_=‘listgroup‘) # # from bs4.element import Tag # # for child in list_group.children: # if isinstance(child, Tag): # project_tag = child.find(name=‘a‘, class_=‘mr-1‘) # size_tag = child.find(name=‘small‘) # temp = "項目:%s(%s); 項目路徑:%s" % (project_tag.get(‘href‘), size_tag.string, project_tag.string, ) # print(temp) github
示例

自動登錄知乎

技術分享圖片
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import time

import requests
from bs4 import BeautifulSoup

session = requests.Session()

i1 = session.get(
    url=https://www.zhihu.com/#signin,
    headers={
        User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36,
    }
)

soup1 = BeautifulSoup(i1.text, lxml)
xsrf_tag = soup1.find(name=input, attrs={name: _xsrf})
xsrf = xsrf_tag.get(value)

current_time = time.time()
i2 = session.get(
    url=https://www.zhihu.com/captcha.gif,
    params={r: current_time, type: login},
    headers={
        User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36,
    })

with open(zhihu.gif, wb) as f:
    f.write(i2.content)

captcha = input(請打開zhihu.gif文件,查看並輸入驗證碼:)
form_data = {
    "_xsrf": xsrf,
    password: xxooxxoo,
    "captcha": captcha,
    email: [email protected]
}
i3 = session.post(
    url=https://www.zhihu.com/login/email,
    data=form_data,
    headers={
        User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36,
    }
)

i4 = session.get(
    url=https://www.zhihu.com/settings/profile,
    headers={
        User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36,
    }
)

soup4 = BeautifulSoup(i4.text, lxml)
tag = soup4.find(id=rename-section)
nick_name = tag.find(span,class_=name).string
print(nick_name)

知乎
示例

自動登錄博客園

技術分享圖片
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re
import json
import base64

import rsa
import requests


def js_encrypt(text):
    b64der = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCp0wHYbg/NOPO3nzMD3dndwS0MccuMeXCHgVlGOoYyFwLdS24Im2e7YyhB0wrUsyYf0/nhzCzBK8ZC9eCWqd0aHbdgOQT6CuFQBMjbyGYvlVYU2ZP7kG9Ft6YV6oc9ambuO7nPZh+bvXH0zDKfi02prknrScAKC0XhadTHT3Al0QIDAQAB
    der = base64.standard_b64decode(b64der)

    pk = rsa.PublicKey.load_pkcs1_openssl_der(der)
    v1 = rsa.encrypt(bytes(text, utf8), pk)
    value = base64.encodebytes(v1).replace(b\n, b‘‘)
    value = value.decode(utf8)

    return value


session = requests.Session()

i1 = session.get(https://passport.cnblogs.com/user/signin)
rep = re.compile("‘VerificationToken‘: ‘(.*)‘")
v = re.search(rep, i1.text)
verification_token = v.group(1)

form_data = {
    input1: js_encrypt(wptawy),
    input2: js_encrypt(asdfasdf),
    remember: False
}

i2 = session.post(url=https://passport.cnblogs.com/user/signin,
                  data=json.dumps(form_data),
                  headers={
                      Content-Type: application/json; charset=UTF-8,
                      X-Requested-With: XMLHttpRequest,
                      VerificationToken: verification_token}
                  )

i3 = session.get(url=https://i.cnblogs.com/EditDiary.aspx)

print(i3.text)

博客園
示例

基於Requests和BeautifulSoup實現“自動登錄”