前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >模式登陆的两种方式

模式登陆的两种方式

作者头像
用户2337871
发布2020-05-06 14:35:34
5850
发布2020-05-06 14:35:34
举报
文章被收录于专栏:git

模式登陆的两种方式

1.request+session

1.1:

代码语言:javascript
复制
import requests

from urllib.parse import urljoin

BASE_URL = "https://login2.scrape.cuiqingcai.com"
LOGIN_URL = urljoin(BASE_URL, '/login')
INDEX_URL = urljoin(BASE_URL, '/page/1')

USERNAME = "admin"
PASSWORD = "admin"

response_login = requests.post(LOGIN_URL, data={
    "username": USERNAME,
    "password": PASSWORD
}, allow_redirects=False)

cookies = response_login.cookies
print(cookies)

response_index = requests.get(INDEX_URL, cookies=cookies)

print(response_index.status_code)
print(response_index.url)

1.2:

代码语言:javascript
复制
import requests

from urllib.parse import urljoin

BASE_URL = "https://login2.scrape.cuiqingcai.com"
LOGIN_URL = urljoin(BASE_URL, '/login')
INDEX_URL = urljoin(BASE_URL, '/page/1')

USERNAME = "admin"
PASSWORD = "admin"

session=requests.Session()

response_login = requests.post(LOGIN_URL, data={
    "username": USERNAME,
    "password": PASSWORD
})

cookies = session.cookies
print(cookies)

response_index = requests.get(INDEX_URL)

print(response_index.status_code)
print(response_index.url)

2. selenium+session

代码语言:javascript
复制
import requests
import time
from urllib.parse import urljoin
from selenium import webdriver

BASE_URL = "https://login2.scrape.cuiqingcai.com"
LOGIN_URL = urljoin(BASE_URL, '/login')
INDEX_URL = urljoin(BASE_URL, '/page/1')

USERNAME = "admin"
PASSWORD = "admin"

browser = webdriver.Chrome()
browser.get(BASE_URL)
browser.find_element_by_css_selector('input[name="username"]').send_keys(USERNAME)
browser.find_element_by_css_selector('input[name="password"]').send_keys(PASSWORD)
browser.find_element_by_css_selector('input[type="submit"]').click()
time.sleep(10)

cookies = browser.get_cookies()
print(cookies)
browser.close()

session = requests.Session()
for cookie in cookies:
    session.cookies.set(cookie['name'], cookie['value'])

response_index = session.get(INDEX_URL)

print(response_index.status_code)
print(response_index.url)

3.jwt--模拟登录

代码语言:javascript
复制
import requests
from urllib.parse import urljoin

BASE_URL = 'https://login3.scrape.cuiqingcai.com/'
LOGIN_URL = urljoin(BASE_URL, '/api/login')
INDEX_URL = urljoin(BASE_URL, '/api/book')
USERNAME = 'admin'
PASSWORD = 'admin'

response_login = requests.post(LOGIN_URL, json={
    'username': USERNAME,
    'password': PASSWORD
})
data = response_login.json()
print('Response JSON', data)
jwt = data.get('token')
print('JWT', jwt)

headers = {
    'Authorization': f'jwt {jwt}'
}
response_index = requests.get(INDEX_URL, params={
    'limit': 18,
    'offset': 0
}, headers=headers)
print('Response Status', response_index.status_code)
print('Response URL', response_index.url)
print('Response Data', response_index.json())
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档