Skip to content

Commit

Permalink
Get cookies via selenium.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lodour committed Mar 11, 2019
1 parent bc32b2b commit d9033ff
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
2. 设置`settings.py`

* `STORE_PATH` 下载目录
* `COOKIES` 任意用户微博的cookies
* `COOKIES` 任意用户微博的 Cookies,或设置为 `AUTO` 以自动获取(需安装 ChromeDriver)
* `TARGETS` 目标用户的微博主页url

3. 运行
Expand Down
15 changes: 4 additions & 11 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
appdirs==1.4.0
certifi==2017.7.27.1
chardet==3.0.4
colorama==0.3.9
futures==3.1.1
idna==2.6
packaging==16.8
pyparsing==2.1.10
requests==2.18.4
six==1.10.0
urllib3==1.22
colorama==0.4.1
futures
requests==2.21.0
selenium==3.141.0
2 changes: 1 addition & 1 deletion settings.sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# 存储目录
STORE_PATH = './downloads'

# 在这里粘贴你的cookies
# 在这里粘贴你的 Cookies,或设置为 `AUTO` 以自动获取(需安装 ChromeDriver)
COOKIES = 'SINAGLOBAL=...; UM_distinctid=...; __guid=...; ...'

# 在这里添加目标用户的微博主页url
Expand Down
4 changes: 4 additions & 0 deletions weibo/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import requests

import settings
from weibo.browser import WeiboBrowser


class Url(object):
Expand All @@ -28,6 +29,9 @@ def _load_cookies():
从设置中解析cookies
:return: dict
"""
if settings.COOKIES == 'AUTO':
with WeiboBrowser() as weibo:
return weibo.get_cookies()
assert settings.COOKIES, '请在`settings.py`中粘贴cookies'
return dict([l.split('=', 1) for l in settings.COOKIES.split('; ')])

Expand Down
36 changes: 36 additions & 0 deletions weibo/browser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.expected_conditions import visibility_of_element_located


class WeiboBrowser(object):

def __init__(self):
self._driver = webdriver.Chrome()

def get_cookies(self):
# 主页
self._driver.get('http://weibo.com')
# 二维码登陆框
qrcode_tab = self._wait_element('//a[@node-type="qrcode_tab"]')
qrcode_tab.click()
# 二维码
qrcode_img = self._wait_element('//img[@node-type="qrcode_src"]')
# 登陆
self._wait_element('//a[@class="gn_name"]', 300)

cookies = {c['name']: c['value'] for c in self._driver.get_cookies()}
return cookies

def _wait_element(self, xpath, timeout=60):
locator = (By.XPATH, xpath)
condition = visibility_of_element_located(locator)
element = WebDriverWait(self._driver, 60).until(condition)
return element

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self._driver.quit()

0 comments on commit d9033ff

Please sign in to comment.