def test_fail_bost(self, testdata): login = Login(self.driver) login.login(testdata["username"],testdata["password"]) time.sleep(3) real = login.get_title() hope = testdata["hope"] self.assertEqual(real, hope)
def DoTasker(): lstUserData = Data.laodUserData() emails = [] for d in lstUserData: login = Login(d[0], d[1]) skey, reason = login.autologin() if skey == False: # 签到失败,删除用户信息,发送邮件提醒 email = Data.getUserEmailData(d[0]) if email != None and len(email) > 0: emails.append(email) Data.removeUserData(d[0]) Data.removeUserEmailData(d[0]) sign = Sign(d[0], skey) if sign.autosign() == False: pass # time.sleep(1) # print emails if len(emails) > 0: email = libs.mail.Message(Config.smtp_user, emails, Config.mail_notify) try: conn = libs.mail.Connection(Config.smtp_server, 25, Config.smtp_user, Config.smtp_pass) conn.send_message(email) except: pass
def main (): #make sure to shown tacskIcon right when use pyinstaller to make exe file you can commont it ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("BatterayDetection") me = singleton.SingleInstance() app = QtGui.QApplication(sys.argv) splash = QSplashScreen(QPixmap(":/Start/Resource/start.png"), Qt.WindowStaysOnTopHint) splash.setDisabled(True) # Disables widget input events, or when mouse click splash will be hide splash.show() splash.showMessage("Start Application ......", Qt.AlignBottom, Qt.black) app.processEvents() start = time() while time() - start < 1: sleep(0.1) app.processEvents() login = Login() #login.show() splash.finish(login) if (login.exec_() == QtGui.QDialog.Accepted) : main = MainWindow() main.show() sys.exit(app.exec_())
def change_password(self): #pylint:disable=missing-docstring,wrong-import-order,trailing-whitespace, multiple-imports try: a = Login(user=self.user, password=self.pswd) #pylint:disable=invalid-name session = a.get_session() change_password_html = session.get(CHANGE_PASSWORD_PAGE) soup_data = change_password_html.text soup = BeautifulSoup(soup_data, "html.parser") state = soup.find(id="__VIEWSTATE") viewstate = state["value"] payload = { "__EVENTTARGET": "", "__EVENTARGUMENT": "", " __VIEWSTATE": viewstate, "txtOldPassword": self.pswd, "txtNewPassword": self.new_pswd, "txtConfirmPassword": self.new_pswd, "btnUpdate": "Change Password", "hdPasswordLength": 6, "hdRollid": 3 } session.post(CHANGE_PASSWORD_PAGE, data=payload, verify=False) except: #pylint:disable=bare-except traceback.print_exc(file=sys.stdout)
def DoTasker(): lstUserData = Data.laodUserData() emails = [] for d in lstUserData: login = Login(d[0], d[1]) skey, reason = login.autologin() if skey == False: #签到失败,删除用户信息,发送邮件提醒 email = Data.getUserEmailData(d[0]) if email != None and len(email) > 0: emails.append(email) Data.removeUserData(d[0]) Data.removeUserEmailData(d[0]) sign = Sign(d[0], skey) if sign.autosign() == False: pass #time.sleep(1) #print emails if len(emails) > 0: email = libs.mail.Message(Config.smtp_user, emails, Config.mail_notify) try: conn = libs.mail.Connection(Config.smtp_server, 25, Config.smtp_user, Config.smtp_pass) conn.send_message(email) except: pass
def LoginActivity(email, password): firebase = FB.Config(Settings.settings) user = FB.UserLogin(email=email, password=password, cfg=firebase) try: session["user"] = user.auth.get_account_info( user.user["idToken"])["users"] except Exception as e: print(e)
def callback(): login = Login() username = usuario.get() password = contrasena.get() if login.verificar(username, password) == "True": message.configure(text="Bienvenido") from AltasBajasGUI import AltasGUI else: message.configure( text="Nombre de usuario o contraseña incorrectos")
def loginProcess(self): self.id = ui.log_idInput.text() password = ui.log_passInput.text() self.login = Login(self.conn) self.login.checkResult(self.id, password) if self.login.loginResult == 1: ui.stackedWidget.setCurrentIndex(1) self.showList() elif self.login.loginResult == 2: self.login_alarm("아이디 혹은 비밀번호를 다시 입력해주세요")
def instBot(): work_done = False while not work_done: user = UserDetails() selenium = SeleniumDetails() selenium.getDriver().get("https://www.instagram.com") login = Login(user, selenium) if login.main(): profile = Profile(user, selenium) if profile.main(): work_done = True selenium.getDriver().quit()
def __init__(self, email, password): self.headers = { 'Referer': 'https://github.com/', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36', 'Host': 'github.com' } self.email = email self.password = password self.foller_url = 'https://github.com/{name}?tab=followers' self.login = Login() self.session = self.login.login(email, password) self.set = set()
def get_categories_and_save_to_db(): login = Login() login.login() a_elements = driver.find_elements_by_xpath( "//table[@class='categories']//child::a") categories = [] for a_element in a_elements: link_href = a_element.get_attribute('href') category_id = int((link_href.split('?')[-1]).split("=")[-1]) category_name = a_element.get_attribute('text') new_category = Category(category_name, category_id) categories.append(new_category) database.insert_category(category_id, category_name) return categories
def login(): form = LoginForm(request.form) #get username / password username = form.username.data password = form.password.data accounts = root.child('accounts').get() list = [] for accountid in accounts: eachaccount = accounts[accountid] if eachaccount['account type'] == 'iuser' or eachaccount[ 'account type'] == 'idoctor' or eachaccount[ 'account type'] == 'iinstructor': account = Login(eachaccount['account type'], eachaccount['username'], eachaccount['password']) account.set_registerid(accountid) list.append(account) #--------------------------------------------------------- #-------------------------------------------------------- if request.method == 'POST' and form.validate(): username_list = [] password_list = [] while True: for result in list: username_list.append(result.get_username()) password_list.append(result.get_password()) print(username_list) print(password_list) break if username in username_list and password in password_list: if username_list.index(username) == password_list.index(password): session['logged_in'] = True return redirect(url_for('view_Booking_Page')) else: error = 'Invalid login' flash(error, 'danger') return render_template('login.html', form=form) # print("Username:"******"Password:",account.get_password()) # if username == account.get_username() and password == account.get_password(): # session['logged_in'] = True # return redirect(url_for('view_Booking_Page')) # else: # error = 'Invalid login' # flash(error,'danger') # return render_template('login.html',form=form) return render_template('login.html', form=form)
def login(): while True: os.system("cls") print("[ LOGIN ]") username = input("ENTER USERNAME >> ") password = getpass.getpass("ENTER PASSWORD >> ") user = Login(username, password) if user.valid_user(): print("Login Successful!") time.sleep(1) break else: print("Login Unsuccessful, try again..") time.sleep(1) continue
def run_login(self, widget): # Seleccionar un Usuario o llama a crear uno nuevo. from Login import Login login = Login() login.connect("crear_usuario", self.crear_usuario) login.connect("load_usuario", self.run_menu) login.run() return False
def test_search_006(self): """测试搜索搜索功能""" Login(self.wd).test_login002("13253728975", "123456w") Base(self.wd).ClickElement(By.XPATH, '//*[@id="app"]/div[2]/div[2]/div[4]') Base(self.wd).ClickElement(By.XPATH, '//*[@id="app"]/div[2]/div[1]/div[1]/div') time.sleep(2) Base(self.wd).sendKeys( By.CSS_SELECTOR, '#app > div:nth-child(2) > div.search-input > div > div > div > input[type=text]', '朝花夕拾') time.sleep(2) Base(self.wd).ClickElement(By.XPATH, '//*[@id="app"]/div[2]/div[1]/div/span[2]') all = WebDriverWait( self.wd, 30, 1).until(lambda x: x.find_elements(By.CSS_SELECTOR, '.item-title')) text = all[1].text print(text) try: self.wd.find_element_by_xpath("//*[contains(text(),'朝花夕拾')]") except: return False
def test_search_007(self): """测试阅读页面是否存在""" Login(self.wd).test_login002("13253728975", "123456w") Base(self.wd).ClickElement(By.XPATH, '//*[@id="app"]/div[2]/div[2]/div[4]') Base(self.wd).ClickElement(By.XPATH, '//*[@id="app"]/div[2]/div[1]/div[1]/div') print('点击输入框') time.sleep(2) # Base.(self.wd).sendKeys(By.CSS_SELECTOR,'#app > div:nth-child(2) > div.search-input > div > div > div > input[type=text]') Base(self.wd).sendKeys( By.CSS_SELECTOR, '#app > div:nth-child(2) > div.search-input > div > div > div > input[type=text]', '朝花夕拾') print('输入') Base(self.wd).ClickElement(By.XPATH, '//*[@id="app"]/div[2]/div[1]/div/span[2]') time.sleep(2) Base(self.wd).ClickElement( By.XPATH, '//*[@id="app"]/div[2]/div[2]/div/div[1]/div/ul[1]/div/li/div/div[2]' ) time.sleep(2) text = Base(self.wd).text(By.CSS_SELECTOR, '.author') assert text == '鲁迅'
def test_search_009(self): """测试--搜索历史--""" Login(self.wd).test_login002("13253728975", "123456w") Base(self.wd).ClickElement(By.XPATH, '//*[@id="app"]/div[2]/div[2]/div[4]') time.sleep(1) Base(self.wd).ClickElement(By.XPATH, '//*[@id="app"]/div[2]/div[1]/div[1]/div') print('点击输入框') time.sleep(2) Base(self.wd).sendKeys(By.CSS_SELECTOR, '.input-container>input', '朝花夕拾') print('输入') time.sleep(1) # 点击搜索 Base(self.wd).ClickElement(By.XPATH, '//*[@id="app"]/div[2]/div[1]/div/span[2]') print('sou') time.sleep(2) Base(self.wd).ClickElement( By.CSS_SELECTOR, '#app div:nth-child(2) > div.search-input > div > span.cancel') time.sleep(1) Base(self.wd).ClickElement(By.XPATH, '//*[@id="app"]/div[2]/div[1]/div[1]/div') # time.sleep(4) all = WebDriverWait(self.wd, 30, 1).until(lambda x: x.find_elements( By.CSS_SELECTOR, 'div.search-history > ul > li:nth-child(1) > span' )) all[0].click() time.sleep(1) text = Base(self.wd).text(By.CSS_SELECTOR, '.author') assert text == '鲁迅'
def test_search_005(self): """测试搜索取消功能""" Login(self.wd).test_login002("13253728975", "123456w") Base(self.wd).ClickElement(By.XPATH, '//*[@id="app"]/div[2]/div[2]/div[4]') time.sleep(1) Base(self.wd).ClickElement( By.CSS_SELECTOR, '#app>div:nth-child(2)>div:nth-child(1)>div.discover-search>div') print(5) time.sleep(2) Base(self.wd).sendKeys( By.CSS_SELECTOR, '#app > div:nth-child(2) > div.search-input > div > div > div > input[type=text]', '朝花夕拾') time.sleep(2) Base(self.wd).ClickElement( By.CSS_SELECTOR, '#app>div:nth-child(2)>div.search-input>div>span.cancel') print(5) text = Base(self.wd).text( By.CSS_SELECTOR, '#app > div:nth-child(2) > div:nth-child(1) > div.van-list > div:nth-child(1) > div:nth-child(1) > div.list-title > span' ) assert text == '根据《零点超控》推荐'
class Solution: newUserId = 1110 print("1.LOGIN \n 2.SIGNUP") opt = int(input("enter the option")) if (opt == 1): user = "******" userName = input("enter the username") userType = input("enter the usertype:(Admin/Student)") password = input("enter the password") oldUser = Login(user, userName, userType, password) else: user = "******" userName = newUserId + 1 userType = input("enter the usertype(STUDENT/ADMIN):") password = input("set the password") newUser = Login(user, userName, userType, password)
def __init__(self, *args, **kwargs): # __init__ function for class Tk tk.Tk.__init__(self, *args, **kwargs) # creating a container and setting the layout, which can change the frames based on user activity container = tk.Frame(self) container.pack(side="top", fill="both", expand=True) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) # Below mentioned are the list of frames which are called upon with the button press, we are sending the # sending the value of new window as string so that there would be new window populated basing on the user event self.frames = { "Login": Login(parent=container, controller=self), "HomePage": HomePage(parent=container, controller=self), "RegisteringProducts": RegisteringProducts(parent=container, controller=self), "IndividualBilling": IndividualBilling(parent=container, controller=self), "ShippingProducts": ShippingProducts(parent=container, controller=self) } # Aligning the layout of all available frame windows, row=0, column=0 implies they all be on same position self.frames["Login"].grid(row=0, column=0, sticky="nsew") self.frames["HomePage"].grid(row=0, column=0, sticky="nsew") self.frames["RegisteringProducts"].grid(row=0, column=0, sticky="nsew") self.frames["IndividualBilling"].grid(row=0, column=0, sticky="nsew") self.frames["ShippingProducts"].grid(row=0, column=0, sticky="nsew") # First window to be shown when user opens app self.show_frame("Login")
def __init__(self): """ 初始化相关参数 """ self.login = Login() self.login.login() self.headers = self.login.getHeaders() self.data = self.login.getData() self.session = self.login.getSession() # self.session.encoding = 'utf8' # print self.session. # data link list self.havefinished_list = [] # 已经处理的数据连接 self.waitting_list = [] # 未处理的数据连接 self.record_topic_data = dict() self.record_topic_link_data = dict()
def configure_game(): global root update_window_size(window, 800, 450) if root is not None: root.destroy() root = tk.Frame() Login(root, auth, configure_menu)
def login(self, username, password=Tzjconfig.password, url=Tzjconfig.login_url): ''' 登录,获取token,这里需填写用户名,password默认是test1111,url有默认值 :param username: :param password: :param url: :return: ''' ll = Login() backdata = ll.tzj_login(url=url, username=username, password=password) if backdata.has_key('token'): self.token = backdata['token'] return self.token else: print U'无法获取token' return self.token
def test_view__all_classes(self): a = App(Login(DjangoInterface()), UserEdits(), CourseEdit()) result = a.command("LoggedInUser") # Error cases self.assertEqual("Not enrolled in any classes", result) # Success self.assertEqual("List of classes: ", result)
def show_go_screen(): fondo_menu = pygame.transform.scale(pygame.image.load("recursos/background.png"), [800, 600]) screen.blit(fondo_menu, [0, 0]) draw_text(screen, "Wing Gundam Zero", 65, WIDTH // 2, HEIGHT // 4) draw_text(screen, "Press Esc to return main menu", 20, WIDTH // 2, HEIGHT * 3/4) draw_text(screen, "Press Enter to play", 20, WIDTH // 2, HEIGHT // 2) pygame.display.flip() waiting = True while waiting: clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.mixer_music.stop() pygame.quit() if event.type == pygame.KEYUP: if event.key == pygame.K_RETURN: waiting = False if event.key == pygame.K_ESCAPE: pygame.mixer_music.stop() login = Login() menu = Game(documento_usuario, nombre, nombre_usuario, correo, fecha_nacimiento, pais, login) menu.menu_principal() pygame.quit() sys.exit()
class Teste: l = Login() while true: try: l.CallMenu(l.Validation()) except ValueError: print("Login Inválido")
def test_ViewDatabase(self): a = App(Login(DjangoInterface()), UserEdits(), CourseEdit()) result = a.command("LoggedInUser") # Error cases self.assertEqual("Illegal permissions to do this activity", result) self.assertEqual("Error while connecting to the database", result) # Success self.assertEqual("Data gathered", result)
def test_login(self): driver = self.driver driver.maximize_window() driver.get(self.base_url) """登录""" Login().user_login(driver) """退出""" Logout().user_logout(driver)
def main(): # Basic response to begin conversation #first perform a login obj = Reminder() mailobj = Mail() global counter global name global username log = Login() name, username = log.logger() #print("USERNAME="******" ".join(re.findall("[a-zA-Z0-9]+", str(name))) if name != '': counter = 0 if counter == 1: #Add name to database yet to implement print("Welcome to ChatterBox. What is your name?") name = input() print("Hello " + name + "\n I'm Alice") if counter == 0: print('Hello ' + name + ". Welcome back. Here are todays reminders...") #os.system('play --no-show-progress --null --channels 1 synth %s sine %f' % (1, 500)) time.sleep(0.5) obj.RemindersToday(username) print("Here are your emails") time.sleep(2) mailobj.LatestMails() time.sleep(1) print("How can I help you?") #reception of inputs, for each input, perform processing to understand what the input requires. Statement = input() Statement = Statement.lower() # print(Statement) reply = Statement counter = 0 global cond cond = 0 #start giving replies while process(Statement) != -1: #preprocessing done Statement = input("How can I help you?\n")
def test_Add_project(self): """新建长租产品""" driver = self.driver driver.maximize_window() driver.get(self.base_url) Login().user_login(driver) Add_Project().Add_project(driver) sleep(10)
def logout(self): """Log out and show the login screen.""" if self.user is not None: self.startDb.logEntry(self.user[0], 2, None, self.cursor) self.connection.commit() self.user = None self.setWindowTitle(self.getVersion()) widget = Login(self, self) self.changeDisplay(widget)
def test_send__TA_email(self): a = App(Login(DjangoInterface()), UserEdits(), CourseEdit()) result = a.command("From To Subject Body TA UpdatedUser") # Error cases self.assertEqual("Illegal permissions to do this activity", result) self.assertEqual("TA(s) do not exist", result) self.assertEqual("Illegal email subject", result) self.assertEqual("Illegal email body", result) # Success self.assertEqual("Email sent to TA(s) successfully", result)
def __init__(self): http_client.HTTPConnection.debuglevel = 0 try: Logging.info("geting cookies") self.requests = requests.Session() self.requests.cookies = cookielib.LWPCookieJar("zhihu_api/cookies") self.cookies = self.requests.cookies self.requests.cookies.load(ignore_discard=True) Login.islogin() except NotLogin: Login.login() except: Logging.error(u"找不到cookie") reload(sys) sys.setdefaultencoding("utf8") Logging.info("Default encoding: " + sys.getdefaultencoding()) self.proxies = {"http": "http://127.0.0.1:8080", "https": "http://127.0.0.1:8080"}
def login(s): l = Login() l.username = USERNAME l.password = PASSWORD l.platform = PLATFORM l.version = VERSION l.protocol = PROTOCOL s.send(str(l))
def nsuite(): n = unittest.TestSuite() n.addTest(Signup.SignUp("test")) n.addTest(General.general()) n.addTest(Lists.lists()) n.addTest(Views.views()) n.addTest(Subtasks.subtasks()) n.addTest(Files.files()) n.addTest(Recurrence.recurrence()) n.addTest(Positioning.positioning()) n.addTest(DefaultFolder.default_folder()) n.addTest(Sharing.sharing()) n.addTest(ListSharing.list_sharing()) n.addTest(Login.login()) return n
def main(): SEND_MSG = ("[自动回复]这里是机器人,稍后回复您的消息," "抱歉.(望勿频繁调戏机器人...)") bot = Login() LOG.info('请扫描二维码.') print(bot.get_QRcode()) os.remove('QRcode.png') Is_login = False while not Is_login: time.sleep(2) res = bot.is_login().split(',')[-2] LOG.info(res) Is_login = True if '登录成功' in res else False LOG.info('获取ptwebqq...') bot.get_ptwebqq() LOG.info('获取vfwebqq...') bot.get_vfwebqq() LOG.info('获取psessionid...') bot.get_psessionid() LOG.info('等待消息...') STOP = False is_open = True while not STOP: time.sleep(1) try: msg = bot.poll() msg_content, from_uin, msg_type = hand_msg(msg[0]) LOG.info('{0} 发来一条消息: {1}'.format(from_uin, msg_content.encode('utf-8'))) if (is_open is True) and ('STOP' not in msg_content): msg = "{0}[{1}]".format(SEND_MSG, random.randint(0, 10)) send_status = bot.send_msg(msg, from_uin, msg_type) LOG.info('回复 {0}: {1}'.format(from_uin, send_status)) elif 'STOP' in msg_content: LOG.info('CLOSE...') bot.send_msg('CLOSED', from_uin, msg_type) is_open = False elif 'START' in msg_content: LOG.info('STARTED') bot.send_msg('OPENED', from_uin, msg_type) is_open = True except KeyboardInterrupt: LOG.info('See You...') STOP = True except: LOG.error(sys.exc_info())
# -*-coding: UTF-8-*- import time,re from HotTopic import HotTopic from Login import Login login = Login('yourUserName','yourPWD') login.login() #每天的话题 '''' topic = HotTopic(login.gsid) topic.hot_topic() ''' # '''''' #搜话题 topic = HotTopic(login.gsid) for eachline in file('key.txt','r'): print eachline print type(eachline) topic.linkProducer(eachline.strip())
def testUsuarioCorrecto(self): login = Login() self.assertEqual("True", login.verificar("juanPerez", "cartmancuco"))
def testUsuaioIncorrecto(self): login = Login() self.assertEqual( "False", login.verificar("juanitoMartinez", "noExisto"))
#-*-coding=utf-8 import sys,time sys.path.append(r'/home/irene/Documents/python/Class') sys.path.append(r'/home/irene/Documents/python/Class/System') from Login import Login from User import User from selenium import webdriver driver = webdriver.Firefox() #~~~~~~~~~~~~~~~~~~~~~~~~登录~~~~~~~~~~~~~~~~~~~~~~~~~ #Login(ip,username,password) l = Login("192.168.88.183","admin","admin",driver) l.test_login() #~~~~~~~~~~~~~~~~~~~~~~~~登录成功~~~~~~~~~~~~~~~~~~~~~ #~~~~~~~~~~~~~~~~~~~~~~~~注册用户~~~~~~~~~~~~~~~~~~~~~ #User(driver) #AddUser(name,password,email,telephone,privilege) #privilege---1:系统权限;2:上级监控;3:备份恢复;4:查看报表 u = User(driver) #u.AddUser("kkkkkk3","dingjia123","*****@*****.**","123456789","14") #time.sleep(5) #u.DeleteUser("kkkkkk3",'n') #u.LockUser("dingjia",'y') #u.UnlockUser("dingjia",'y') u.ModifidUser('dingjia','','','4') #~~~~~~~~~~~~~~~~~~~~~~~~注册成功~~~~~~~~~~~~~~~~~~~~~ #~~~~~~~~~~~~~~~~~~~~~~~~客户端访问~~~~~~~~~~~~~~~~~~~ #VisitedCient(user,clients),client多客户端由;号分开 #u.VisitedCient(u,"dingjia","localhost.localdomain_192.168.88.215;rhel5_mysql_216")
class SpiderTopicData(object): """ 获取知乎数据 """ def __init__(self): """ 初始化相关参数 """ self.login = Login() self.login.login() self.headers = self.login.getHeaders() self.data = self.login.getData() self.session = self.login.getSession() # self.session.encoding = 'utf8' # print self.session. # data link list self.havefinished_list = [] # 已经处理的数据连接 self.waitting_list = [] # 未处理的数据连接 self.record_topic_data = dict() self.record_topic_link_data = dict() def setRootTopic(self,root_topic_id='19778317'): self.waitting_list.append(root_topic_id) def relogin(self): """ 短线重连 :return: """ self.login.login() self.headers = self.login.getHeaders() self.data = self.login.getData() self.session = self.login.getSession() def getLinkTopic(self,link_url='https://www.zhihu.com/topic/19778317/organize/entire?parent=19778317'): """ 获取连接下的数据 :param link_url: :return: 数据 text """ # self.session try: res = self.session.post(link_url,data=self.data,headers=self.headers) time.sleep(random.randint(4,12)) except: self.relogin() res = self.session.post(link_url,data=self.data,headers=self.headers) time.sleep(random.randint(4,12)) topic = json.loads(res.text) # topic = eval(res.text) cur_topic = topic['msg'][0] sub_topics = topic['msg'][1] # for test parent_topic_name = cur_topic[1].encode('utf8') parent_topic_id = cur_topic[2] sub_topics_name = [] sub_topics_id = [] for sub in sub_topics: sub_topics_id.append(sub[0][2]) sub_topics_name.append(sub[0][1].encode('utf8')) result = dict() result["parent_topic_id"] = parent_topic_id result["parent_topic_name"] = parent_topic_name result["sub_topics_name"] = sub_topics_name result["sub_topics_id"] = sub_topics_id return result def recordData(self,result): """ 处理爬取的topic 数据,并记录 :param result: :return: """ sub_topics_id = result['sub_topics_id'] sub_topics_name = result['sub_topics_name'] parent_topic_name = result['parent_topic_name'] parent_topic_id = result['parent_topic_id'] existed = False child_topic_id = '' if parent_topic_id not in self.record_topic_data: self.record_topic_data[parent_topic_id] = parent_topic_name for sub_id,sub_name in zip(sub_topics_id,sub_topics_name): if sub_name == str("加载更多"): existed = True child_topic_id = sub_id continue if sub_id not in self.record_topic_data: self.record_topic_data[sub_id] = sub_name if sub_id not in self.record_topic_link_data: self.record_topic_link_data[sub_id] = [] self.record_topic_link_data[sub_id].append(parent_topic_id) return existed,parent_topic_id,child_topic_id def getSubTopic(self,parent_topic_id='19778317',child_topic_id=''): """ 爬取知乎某一话题下的所有子话题,仅爬取话题下一层(即仅爬取当前话题的孩子话题,孙子不管) :param parent_topic_id: 当前话题 ID :param child_topic_id: 子话题 ID应对显示不全时 :return: """ state = True sub_topics_id = [] while state: url_link = 'https://www.zhihu.com/topic/{0}/organize/entire'.format(parent_topic_id) if child_topic_id: url_link += "?child={}&parent={}".format(child_topic_id, parent_topic_id) result = self.getLinkTopic(url_link) sub_topics_id.extend(result['sub_topics_id']) state,parent_topic_id,child_topic_id = self.recordData(result) return sub_topics_id def getAllTopic(self): """ 爬取队列所有话题及子话题 :return: """ while self.waitting_list: topic_id = self.waitting_list.pop(0) if topic_id in self.havefinished_list: continue sub_topics = self.getSubTopic(parent_topic_id=topic_id) self.waitting_list.extend((list(set(sub_topics)))) self.havefinished_list.append(topic_id) print "当前一获取topic number:{}\t 当前已遍历 topic number: {}"\ .format(len(self.havefinished_list)+len(self.waitting_list),len(self.havefinished_list)) self.writeResulttoFile() def writeResulttoFile(self,topic_file=config.TopicFilePath+"zhihu_topic.json",topic_link_file = config.TopicFilePath+'zhihu_topic_link.json'): with codecs.open(topic_file,'w',encoding='utf8') as topic_fp: json.dump(self.record_topic_data,topic_fp,ensure_ascii=False,encoding='utf8') with codecs.open(topic_link_file,'w',encoding='utf8') as topic_link_fp: json.dump(self.record_topic_link_data,topic_link_fp,ensure_ascii=False,encoding='utf8')