示例#1
0
    def __fillFromLink(self,link,session):
        #sys.stderr.write("Filling From [%s]\n" % link)
        page = session.get(link)
        tree            =   html.fromstring(page.text)
        questionIds     =   []
        for divId in tree.xpath('//div[@id]/@id'):
            if divId.startswith("question"):
                splitList = re.split("_",divId)
                if len(splitList) != 2:
                    continue
                try:
                    questionId = int(splitList[1])
                    questionIds.append(questionId)
                except ValueError:
                    pass 
        for questionId in questionIds:
            #sys.stderr.write("QuestionId [%d]\n" % questionId)
            #----------------------------------------------------------------------------
            if not QuestionDB.hasQuestion(questionId):
                text    =   tree.xpath('//div[@id="qtext_%d"]/p/text()' % questionId)[0].encode('ascii','ignore').strip() 

                answers = tree.xpath('//form[@name="answer_%s"]/div/label[@class="radio"]/text()' % questionId)
                QuestionDB.addQuestion(questionId,text,answers)
            #----------------------------------------------------------------------------
            self.Answers.append(questionId)
            #----------------------------------------------------------------------------
        nextLink = tree.xpath('//li[@class="next"]/a/@href')
        if len(nextLink) != 0:
            return nextLink[0]
        else:
            return None
示例#2
0
    def __fillFromLink(self,link,session):
        sys.stderr.write("Filling From [%s]\n" % link)
        page = session.get(link)
        tree            =   html.fromstring(page.text)
        questionIds     =   []
        for divId in tree.xpath('//div[@id]/@id'):
            if divId.startswith("question"):
                splitList = re.split("_",divId)
                if len(splitList) != 2:
                    continue
                try:
                    questionId = int(splitList[1])
                    questionIds.append(questionId)
                except ValueError:
                    pass 
        for questionId in questionIds:
            #sys.stderr.write("QuestionId [%d]\n" % questionId)
            #----------------------------------------------------------------------------
            if not QuestionDB.hasQuestion(questionId):
                try:
                    text    =   tree.xpath('//div[@id="qtext_%d"]/p/text()' % questionId)[0].encode('ascii','ignore').strip() 
                    answers =   tree.xpath('//ul[@id="self_answers_%s"]/li/text()' % questionId)
                    QuestionDB.addQuestion(questionId,text,answers)
                except:
                    continue
            #----------------------------------------------------------------------------
            answer      =   UserProfile.UserAnswer(questionId)
            selected = tree.xpath('//form[@name="answer_%s"]/div/input[@name="my_answer"]' % questionId)

            for idx in range(len(selected)):
                if "checked" in selected[idx].values():
                    answer.Selected = idx+1

            acceptable  = tree.xpath('//form[@name="answer_%s"]/div[@class="container acceptable_answers"]/input[@checked]/@value' % questionId)
            for accept in acceptable:
                if accept == 'irrelevant':
                    continue
                answer.Accepted += (1<<(int(accept)))
            importance = tree.xpath('//form[@name="answer_%s"]/div[@class="container importance"]/div[@class="importance_radios"]/input[@checked]/@value' % questionId)
            if len(importance) == 1:
                answer.Importance = int(importance[0])

            explination = tree.xpath('//textarea[@id="answer_%s_explanation"]/text()' % questionId)
            if len(explination) == 1:
                answer.Explination = explination[0]


            if answer.Selected != 0:
                self.Answers[questionId] = answer
        nextLink = tree.xpath('//li[@class="next"]/a/@href')
        if len(nextLink) != 0:
            return nextLink[0]
        else:
            return None