-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselenium_vbo_focus_account.py
More file actions
160 lines (130 loc) · 6.27 KB
/
selenium_vbo_focus_account.py
File metadata and controls
160 lines (130 loc) · 6.27 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# -*- coding: utf-8 -*-
'''
Created on 2015年12月21日
@author: rhc
'''
import logging,time,sys
from selenium_vbo import SeleniumVbo
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding)
logging.basicConfig(filename='log.log', format='[%(asctime)s]%(levelname)s:%(message)s', filemode='a',stream=True,level='INFO')
logger=logging.getLogger(__name__)
class SeleniumVboFocusAccount(SeleniumVbo):
#关注一个微博账号
def focusWeiboUser(self,nick_name,account):
logger.info("begin focus %s:%s" % (nick_name,account))
self.urlSerachNickName(nick_name)
if not self.searchResultIsExistNickName(nick_name,account):
logger.info("关注 %s 失败" % nick_name)
return False
self.focusAccount(nick_name, account)
logger.info("focus %s:%s success" % (nick_name,account))
return True
#初始化搜索页面
def init(self):
self.get("http://s.weibo.com/")
try:
self.find_element(self.driver.find_element_by_xpath, '//input[@class="searchInp_form"]').send_keys("魏晨")
self.click(self.find_element(self.driver.find_element_by_xpath, '//*[@node-type="submit"]'))
except:
pass
finally:
time.sleep(7)
logger.info('focus init finish')
#是否为找人模式
def isFindPeople(self):
return True if self.find_element(self.driver.find_element_by_xpath, '//a[@action-type="searchItem" and @suda-data="key=tblog_search_user&value=user_user" and @class="cur"]', None, False) else False
#设置为找人模式
def setFindPeople(self):
if self.isFindPeople():
return
for i in range(0,5):
self.click(self.find_element(self.driver.find_element_by_xpath, '//a[@action-type="searchItem" and @suda-data="key=tblog_search_user&value=user_user"]', None, True))
if self.isFindPeople():
return
raise Exception("设置找人模式出错")
#是否为按昵称找
def isFindNickName(self):
if self.isFindPeople() is None:
return False
return True if self.find_element(self.driver.find_element_by_xpath, '//span[@action-type="search" and @action-data="flag=nickname"]/parent::a/parent::li[@class="cur"]', None, False) else False
#设置按昵称查找
def sefFindNickName(self):
self.setFindPeople()
if self.isFindNickName():
return
for i in range(0,5):
self.click(self.find_element(self.driver.find_element_by_xpath, '//span[@action-type="search" and @action-data="flag=nickname"]/parent::a', None, True))
if self.isFindNickName():
return
raise Exception("设置按昵称查找模式出错")
#搜索一个值
def inputSearch(self,key):
searchInput=self.find_element(self.driver.find_element_by_xpath, '//input[@class="searchInp_form"]', None, True)
searchInput.clear()
searchInput.send_keys(key)
self.click(self.find_element(self.driver.find_element_by_xpath, '//a[@class="searchBtn" and @node-type="submit"]', None, True))
#用url搜索key
def urlSerachNickName(self,key):
self.get("http://s.weibo.com/user/&nickname="+key)
for i in range(0,5):
if self.find_element(self.driver.find_element_by_xpath, "//*[@id='pl_common_totalshow']", None, False):
break
time.sleep(5)
time.sleep(2)
#搜索结果中是否包含微博
def searchResultIsExistNickName(self,nick_name,account):
weibo_user=self.find_element(self.driver.find_element_by_xpath, '//a[@suda-data="key=tblog_search_user&value=user_feed_1_name" and @uid="%s"]' % account, None, False)
if weibo_user is None:
logger.info("搜索昵称 %s 未找到微博" % nick_name)
return False
logger.info("搜索昵称 %s 找到 微博" % nick_name)
return True
#关注一个账号
def focusAccount(self,nick_name,account):
logger.info("focus %s:%s" %(nick_name,account))
self.click(self.find_element(self.driver.find_element_by_xpath, '//a[@suda-data="key=tblog_search_user&value=user_feed_1_name" and @uid="%s"]/parent::p/parent::div/parent::div/div[@class="person_adbtn"]/a[@suda-data="key=tblog_search_user&value=user_feed_1_follow"]' % account, None, False))
time.sleep(5)
#从mongo里取出微博并关注
def focusWeiboUers(self,weiboUsers):
logger.info("开始关注微博")
if weiboUsers is None or len(weiboUsers)==0:
logger.info("关注we")
logger.info("关注微博完成")
return
for weiboUser in weiboUsers:
account=weiboUser['account']
result=True
message=''
try:
result=self.focusWeiboUser(weiboUser['nick_name'], account)
message='成功' if result else '未找到微博'
except Exception,e:
logger.error(e)
message=e
finally:
updateInfo={}
updateInfo['status']= 1 if result else 2
#updateInfo['message']=message
updateInfo['again_num']=weiboUser['again_num']+1 if weiboUser.has_key('again_num') else 1
updateInfo['update_time']=time.strftime("%Y-%m-%d %X", time.localtime())
if result:
updateInfo['focus_time']=time.strftime("%Y-%m-%d %X", time.localtime())
self.updateFocusWeiboInfo(account, updateInfo)
logger.info("关注微博完成")
#定时执行
def crond(self):
while True:
logger.info("开始获取需要关注的微博")
weiboUsers=self.getNeedFocusWeibos()
if weiboUsers is None or len(weiboUsers)==0:
logger.info("没有获取到需要关注的微博")
else:
self.focusWeiboUers(weiboUsers)
time.sleep(100)
if __name__ =='__main__':
vbo=SeleniumVboFocusAccount()
vbo.login()
vbo.crond()