-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweibo.py
60 lines (55 loc) · 2.13 KB
/
weibo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import json
import time
from bs4 import BeautifulSoup
import requests
from selenium.common.exceptions import ElementNotVisibleException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
import http.cookiejar as cookielib
agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.20 (KHTML, like Gecko) Chrome/11.0.672.2 Safari/534.20'
headers = {
"Host": "www.zhihu.com",
"Referer": "https://www.zhihu.com/",
'User-Agent': agent
}
driver=webdriver.Chrome("G:\Python3.5\selenium\webdriver\chromedriver.exe")
# driver=webdriver.PhantomJS('G:\Python3.5\Scripts\phantomjs.exe')
driver.get('http://passport.weibo.cn/signin/login')
driver.set_page_load_timeout(3000)
time.sleep(5)
while True:
try:
driver.find_element_by_id('loginName').send_keys('username')
driver.find_element_by_id('loginPassword').send_keys('password')
driver.find_element_by_id('loginPassword').send_keys(Keys.ENTER)
break
except ElementNotVisibleException:
print ("ElementNotVisibleException")
# driver.find_element_by_id('loginName').send_keys('[email protected]')
# driver.find_element_by_id('loginPassword').send_keys('521241')
# driver.find_element_by_id('loginPassword').send_keys(Keys.ENTER)
time.sleep(3)
print("Login Success")
# 获取cookie并通过json模块将dict转化成str
dictCookies = driver.get_cookies()
jsonCookies = json.dumps(dictCookies)
# 登录完成后,将cookie保存到本地文件
with open('weibocookie', 'w') as f:
f.write(jsonCookies)
# driver.delete_all_cookies()
# # 读取登录时存储到本地的cookie
# with open('cookies.json', 'r', encoding='utf-8') as f:
# listCookies = json.loads(f.read())
# for cookie in listCookies:
# driver.add_cookie({
# 'domain': '.xxxx.com', # 此处xxx.com前,需要带点
# 'name': cookie['name'],
# 'value': cookie['value'],
# 'path': '/',
# 'expires': None
# })
print(jsonCookies)
cookie = [item["name"] + "=" + item["value"] for item in driver.get_cookies()]
cookiestr = ';'.join(item for item in cookie)
print (cookiestr)