Created
May 18, 2016 09:37
-
-
Save sincerefly/d7b9f6d780111fd40c7aa8034d022f5a to your computer and use it in GitHub Desktop.
获取QQ群投票信息(需要自己在群中)
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
#!/bin/env python | |
# -*- coding:utf-8 -*- | |
from selenium import webdriver | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.chrome.options import Options | |
from bs4 import BeautifulSoup | |
import time | |
presets = [ | |
{"key":"1080 x 1920","name":"Nexus 5 Portrait","width":1080,"height":1920}, | |
] | |
mobile_emulation = { | |
"deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 }, | |
"userAgent": "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36" } | |
chrome_options = Options() | |
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation) | |
driver = webdriver.Chrome(chrome_options = chrome_options) | |
WAIT_TIME = 30 | |
def login(account): | |
# 访问主页并点击登陆 | |
driver.get("http://qun.qzone.qq.com/") | |
# 等待登陆框加载完毕 | |
driver.switch_to.frame(driver.find_element(By.ID, "ptFrame")) | |
# 填写邮箱与密码登陆 | |
driver.find_element(By.ID, 'u').send_keys(account["username"]) | |
driver.implicitly_wait(WAIT_TIME) | |
driver.find_element(By.ID, 'p').send_keys(account["password"]) | |
driver.implicitly_wait(WAIT_TIME) | |
# 点击登陆 | |
driver.find_element(By.ID, 'login_button').click() | |
driver.implicitly_wait(WAIT_TIME) | |
time.sleep(3) | |
def drag(vote_url): | |
driver.implicitly_wait(WAIT_TIME) | |
# 加载投票基础页面 | |
driver.get(vote_url) | |
driver.implicitly_wait(WAIT_TIME) | |
time.sleep(2) | |
driver.find_element_by_css_selector("li[data-index='1']" ).click() | |
time.sleep(2) | |
# 滑动界面获取全部投票 | |
try: | |
for _ in range(3): | |
js="var q=document.body.scrollTop=10000" | |
driver.execute_script(js) | |
time.sleep(2) | |
except: | |
pass | |
page_source = driver.page_source | |
soup = BeautifulSoup(page_source, 'lxml') | |
span_list = soup.find_all("span", class_="nickname") | |
nickname_list = [] | |
for span in span_list: | |
nickname_list.append(span.string.strip()) | |
result = ',\n'.join(nickname_list) | |
print result | |
driver.quit() | |
def run(): | |
start = time.time() | |
account = { | |
"username": "QQ号", | |
"password": "QQ密码" | |
} | |
vote_url = "投票地址" | |
login(account) | |
drag(vote_url) | |
end = time.time() | |
print end -start | |
if __name__ == '__main__': | |
run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment