Home > Blockchain >  Is there anyways to do efficient coding in bs4
Is there anyways to do efficient coding in bs4

Time:06-13

def parse_table_data(self) -> typing.Union[dict, None]:
    page_source = self.driver.page_source
    soup = BeautifulSoup(page_source, "html.parser")
    svg_container = soup.find_all("div", {"class":"scaledRoad--7fdfb"})

    road_result_container = {
        "A": [],
        "B": [],
        "C": [],
        "D": [],
        "E": [],
        "F": [],
    }

    for tn, c in zip(['A','B','C','D','E','F'], svg_container):
        for svg in c.find_all("svg", {"class": "svg--34293"}):
            if svg.has_attr('name') and svg.has_attr('data-type'):
                name = svg['name']
                data_type = svg['data-type']
                if ("Banker" in name or "Player" in name) and data_type == "roadItem":
                    road_result_container[tn].append(name.split(" ")[0])

    return road_result_container

this code takes about .5 per one function call. I would like to know how to code efficently with bs4. I would think of reducing use of for loop or replacing bs4 to another.

CodePudding user response:

replacing

"html.parser" to "lxml"

really helped me speeding up the code.

  • Related