perceval/backends/core/askbot.py
def parse_answers(html_question):
        """Parse the answers of a given HTML question.

        The method parses the answers related with a given HTML question,
        as well as all the comments related to the answer.

        :param html_question: raw HTML question element

        :returns: a list with the answers
        """

        def parse_answer_container(update_info):
            """Parse the answer info container of a given HTML question.

            The method parses the information available in the answer information
            container. The container can have up to 2 elements: the first one
            contains the information related with the user who generated the question
            and the date (if any). The second one contains the date of the updated,
            and the user who updated it (if not the same who generated the question).

            :param update_info: beautiful soup update_info container element

            :returns: an object with the parsed information
            """
            container_info = {}
            created = update_info[0]
            answered_at = created.abbr.attrs["title"]
            # Convert date to UNIX timestamp
            container_info['added_at'] = str(str_to_datetime(answered_at).timestamp())
            container_info['answered_by'] = AskbotParser.parse_user_info(created)
            try:
                update_info[1]
            except IndexError:
                pass
            else:
                updated = update_info[1]
                updated_at = updated.abbr.attrs["title"]
                # Convert date to UNIX timestamp
                container_info['updated_at'] = str(str_to_datetime(updated_at).timestamp())
                if AskbotParser.parse_user_info(updated):
                    container_info['updated_by'] = AskbotParser.parse_user_info(updated)
            return container_info

        answer_list = []
        # Select all the answers
        bs_question = bs4.BeautifulSoup(html_question, "html.parser")
        bs_answers = bs_question.select("div.answer")
        for bs_answer in bs_answers:
            answer_id = bs_answer.attrs["data-post-id"]
            votes_element = bs_answer.select("div.vote-number")[0].text
            accepted_answer = bs_answer.select("div.answer-img-accept")[0].get('title').endswith("correct")
            # Select the body of the answer
            body = bs_answer.select("div.post-body")
            # Get the user information container and parse it
            update_info = body[0].select("div.post-update-info")
            answer_container = parse_answer_container(update_info)
            # Remove the update-info-container div to be able to get the body
            body[0].div.extract().select("div.post-update-info-container")
            # Override the body with a clean one
            body = body[0].get_text(strip=True)
            # Generate the answer object
            answer = {'id': answer_id,
                      'score': votes_element,
                      'summary': body,
                      'accepted': accepted_answer
                      }
            # Update the object with the information in the answer container
            answer.update(answer_container)
            answer_list.append(answer)
        return answer_list
def parse_answers(html_question):
        def parse_answer_container(update_info):
            """Parse the answer info container of a given HTML question.

            The method parses the information available in the answer information
            container. The container can have up to 2 elements: the first one
            contains the information related with the user who generated the question
            and the date (if any). The second one contains the date of the updated,
            and the user who updated it (if not the same who generated the question).

            :param update_info: beautiful soup update_info container element

            :returns: an object with the parsed information
            """
            container_info = {}
            created = update_info[0]
            answered_at = created.abbr.attrs["title"]
            # Convert date to UNIX timestamp
            container_info['added_at'] = str(str_to_datetime(answered_at).timestamp())
            container_info['answered_by'] = AskbotParser.parse_user_info(created)
            try:
                update_info[1]
            except IndexError:
                pass
            else:
                updated = update_info[1]
                updated_at = updated.abbr.attrs["title"]
                # Convert date to UNIX timestamp
                container_info['updated_at'] = str(str_to_datetime(updated_at).timestamp())
                if AskbotParser.parse_user_info(updated):
                    container_info['updated_by'] = AskbotParser.parse_user_info(updated)
            return container_info

        answer_list = []
        # Select all the answers
        bs_question = bs4.BeautifulSoup(html_question, "html.parser")
        bs_answers = bs_question.select("div.answer")
        for bs_answer in bs_answers:
            answer_id = bs_answer.attrs["data-post-id"]
            votes_element = bs_answer.select("div.vote-number")[0].text
            accepted_answer = bs_answer.select("div.answer-img-accept")[0].get('title').endswith("correct")
            # Select the body of the answer
            body = bs_answer.select("div.post-body")
            # Get the user information container and parse it
            update_info = body[0].select("div.post-update-info")
            answer_container = parse_answer_container(update_info)
            # Remove the update-info-container div to be able to get the body
            body[0].div.extract().select("div.post-update-info-container")
            # Override the body with a clean one
            body = body[0].get_text(strip=True)
            # Generate the answer object
            answer = {'id': answer_id,
                      'score': votes_element,
                      'summary': body,
                      'accepted': accepted_answer
                      }
            # Update the object with the information in the answer container
            answer.update(answer_container)
            answer_list.append(answer)
        return answer_list
def parse_answers ( html_question ) : def parse_answer_container ( update_info ) : """Parse the answer info container of a given HTML question.

            The method parses the information available in the answer information
            container. The container can have up to 2 elements: the first one
            contains the information related with the user who generated the question
            and the date (if any). The second one contains the date of the updated,
            and the user who updated it (if not the same who generated the question).

            :param update_info: beautiful soup update_info container element

            :returns: an object with the parsed information
            """ container_info = { } created = update_info [ 0 ] answered_at = created . abbr . attrs [ "title" ] # Convert date to UNIX timestamp container_info [ 'added_at' ] = str ( str_to_datetime ( answered_at ) . timestamp ( ) ) container_info [ 'answered_by' ] = AskbotParser . parse_user_info ( created ) try : update_info [ 1 ] except IndexError : pass else : updated = update_info [ 1 ] updated_at = updated . abbr . attrs [ "title" ] # Convert date to UNIX timestamp container_info [ 'updated_at' ] = str ( str_to_datetime ( updated_at ) . timestamp ( ) ) if AskbotParser . parse_user_info ( updated ) : container_info [ 'updated_by' ] = AskbotParser . parse_user_info ( updated ) return container_info answer_list = [ ] # Select all the answers bs_question = bs4 . BeautifulSoup ( html_question , "html.parser" ) bs_answers = bs_question . select ( "div.answer" ) for bs_answer in bs_answers : answer_id = bs_answer . attrs [ "data-post-id" ] votes_element = bs_answer . select ( "div.vote-number" ) [ 0 ] . text accepted_answer = bs_answer . select ( "div.answer-img-accept" ) [ 0 ] . get ( 'title' ) . endswith ( "correct" ) # Select the body of the answer body = bs_answer . select ( "div.post-body" ) # Get the user information container and parse it update_info = body [ 0 ] . select ( "div.post-update-info" ) answer_container = parse_answer_container ( update_info ) # Remove the update-info-container div to be able to get the body body [ 0 ] . div . extract ( ) . select ( "div.post-update-info-container" ) # Override the body with a clean one body = body [ 0 ] . get_text ( strip = True ) # Generate the answer object answer = { 'id' : answer_id , 'score' : votes_element , 'summary' : body , 'accepted' : accepted_answer } # Update the object with the information in the answer container answer . update ( answer_container ) answer_list . append ( answer ) return answer_list
Parse the answers of a given HTML question.

        The method parses the answers related with a given HTML question,
        as well as all the comments related to the answer.

        :param html_question: raw HTML question element

        :returns: a list with the answers
Parse the answers of a given HTML question .