<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _collect_nodes(limit, sender, method_name, cacheable, params=None):
 """ Returns a sequence of dom.Node objects about as close to limit as possible """


    if not params:
        params = sender._get_params()

    nodes = []
    page = 1
    end_of_pages = False

    while not end_of_pages and (not limit or (limit and len(nodes) < limit)):
        params["page"] = str(page)

        tries = 1
        while True:
            try:
                doc = sender._request(method_name, cacheable, params)
                break  # success
            except Exception as e:
                if tries >= 3:
                    raise e
                # Wait and try again
                time.sleep(1)
                tries += 1

        doc = cleanup_nodes(doc)

        # break if there are no child nodes
        if not doc.documentElement.childNodes:
            break
        main = doc.documentElement.childNodes[0]

        if main.hasAttribute("totalPages"):
            total_pages = _number(main.getAttribute("totalPages"))
        elif main.hasAttribute("totalpages"):
            total_pages = _number(main.getAttribute("totalpages"))
        else:
            raise Exception("No total pages attribute")

        for node in main.childNodes:
            if not node.nodeType == xml.dom.Node.TEXT_NODE and (
                not limit or (len(nodes) < limit)
            ):
                nodes.append(node)

        if page >= total_pages:
            end_of_pages = True

        page += 1

    return nodes