使用PhantomJS,事先需要下载PhantomJS
from selenium import webdriver
import time
#使用selenium
driver = webdriver.PhantomJS(executable_path="D:\\phantomjs.exe")
driver.maximize_window()
#登录QQ空间
def get_shuoshuo(qq):
driver.get('http://user.qzone.qq.com/{}/311'.format(qq))#说说位于网页的/311界面下
time.sleep(5)
try:
driver.find_element_by_id('login_div')
a = True
except:
a = False
if a == True:
driver.switch_to.frame('login_frame')
driver.find_element_by_id('switcher_plogin').click()
driver.find_element_by_id('u').clear()
#选择用户名框
driver.find_element_by_id('u').send_keys('qq账号')
driver.find_element_by_id('p').clear()
driver.find_element_by_id('p').send_keys('qq密码')
driver.find_element_by_id('login_button').click()
time.sleep(3)
driver.implicitly_wait(3)
try:
driver.find_element_by_id('QM_OwnerInfo_Icon')
b = True
except:
b = False
if b == True:
driver.switch_to.frame('app_canvas_frame')
content = driver.find_elements_by_css_selector('.content')
stime = driver.find_elements_by_css_selector('.c_tx.c_tx3.goDetail')#可根据字段需要匹配多个元素
for con,sti in zip(content,stime):
data = {
'time':sti.text,
'shuos':con.text
}
print(data)
cookie = driver.get_cookies()
cookie_dict = []
for c in cookie:
ck = "{0}={1};".format(c['name'],c['value'])
cookie_dict.append(ck)
i = ''
for c in cookie_dict:
i += c
print('Cookies:',i)
print("==========完成================")
driver.close()
driver.quit()
if __name__ == '__main__':
get_shuoshuo(qq)#需要爬取的qq