0w0

[requests] with requests.Session() as s: 로그인 세션 생성 본문

Coding/Python

[requests] with requests.Session() as s: 로그인 세션 생성

0w0 2019. 8. 7. 15:05
728x90
반응형

- 파이썬 로그인, 세션 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#coing=utf-8
 
import sys
print(sys.version)
from bs4 import BeautifulSoup
import requests
 
 
#로그인 정보
LOGIN_INFO={
#    'email': "IDID",
#    'password': "PWPW"
#    'user_id': 'IDID',
#    'user_pw': 'PWPW',
#    'user-submit': rep.quote_plus('로그인'),
#    'user-cookie': 1
# etc...
}
 
#세션 생성, with 구만안에서만 유지됨.
with requests.Session() as s:
    #LOGIN_INFO를 LOGIN_URI에 보내고 응답을 login_req에 대입
    login_req=s.post('LOGIN_URIURI',data=LOGIN_INFO)
 
    #HTML 소스 확인
    #print('login_req',login_req.text)
 
    #Header 확인
    #print('headers',login_req.headers)
 
    #response 정상확인
    if login_req.status_code==200 and login_req.ok:
        #가져올 내용이 담긴 URI를 요청해서 bring_info에 대입
        bring_info=s.get('URIURI')
 
        #수신 error 예외 발생
        bring_info.raise_for_status()
 
        #BeautifulSoup 호출
        soup=BeautifulSoup(bring_info.text,'html.parser')
        #HTML 행정리된 형태로 출력 확인
        #print(soup.prettify())
 
        #가져올 태그 부분 지정
        statistics = soup.select('TAGTAG')
        #print(statistics)
print(statics.string)
cs

 

 

728x90
반응형
Comments