-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
46 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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 |
This file contains 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
This file contains 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
This file contains 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
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() |