Created
February 24, 2018 06:26
-
-
Save kyujin-cho/7b73e4e0e7644de7b1982f34af28cd76 to your computer and use it in GitHub Desktop.
공감빌런
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bs4 import BeautifulSoup as BSoup | |
import requests | |
like_url = 'https://www.clien.net/service/api/board/park/{}/like' | |
popup_url = 'https://www.clien.net/service/popup/like/park/{}' | |
board = 'https://www.clien.net/service/board/park' | |
LOGIN_INFO = { | |
'userId': 'USERID', | |
'userPassword': 'PASSWORD' | |
} | |
enable_double_like_check = False | |
# 로그인 부분은 https://beomi.github.io/2017/01/20/HowToMakeWebCrawler-With-Login/ 을 참고 | |
with requests.Session() as s: | |
# 우선 클리앙 홈페이지에 들어가 봅시다. | |
first_page = s.get('https://www.clien.net/service') | |
html = first_page.text | |
soup = BSoup(html, 'html.parser') | |
csrf = soup.find('input', {'name': '_csrf'}) # input태그 중에서 name이 _csrf인 것을 찾습니다. | |
print(csrf['value']) # 위에서 찾은 태그의 value를 가져옵니다. | |
# 이제 LOGIN_INFO에 csrf값을 넣어줍시다. | |
# (p.s.)Python3에서 두 dict를 합치는 방법은 {**dict1, **dict2} 으로 dict들을 unpacking하는 것입니다. | |
LOGIN_INFO = {**LOGIN_INFO, **{'_csrf': csrf['value']}} | |
print(LOGIN_INFO) | |
# 이제 다시 로그인을 해봅시다. | |
login_req = s.post('https://www.clien.net/service/login', data=LOGIN_INFO) | |
# 어떤 결과가 나올까요? (200이면 성공!) | |
print(login_req.status_code) | |
soup = BSoup(s.get(board).text, 'html.parser') | |
csrf = soup.find('input', {'name': '_csrf'}) | |
csrf = csrf['value'] | |
print(csrf) | |
headers = { | |
'X-CSRF-TOKEN': csrf, | |
'X-Requested-With': 'XMLHttpRequest', | |
'Host': 'www.clien.net' | |
} | |
for article_id in [x['data-board-sn'] for x in soup.find_all('div', {'data-role': 'list-row'})]: | |
print(article_id, end=': ') | |
if enable_double_like_check: | |
likes = BSoup(s.get(popup_url.format(article_id), headers=headers).text, 'html.parser') | |
if len(list(filter(lambda x: x['onclick'] == "popup.userInfoPopup('basic', '{}')".format(LOGIN_INFO['userId']), likes.find_all('a', {'class': 'nickname'})))) > 0 : | |
print('Already Liked!') | |
continue | |
res = s.post(like_url.format(article_id), headers=headers) | |
print('Liked') | |
# input() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment