哪一个与webDriver中BeautifulSoup的find_u2;(findTag,class_2;=findValue)相同?

2024-03-29 14:09:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用Selenium中BeautifulSoup的find方法find(findClass,class=findValue),我搜索了但是没有我需要的,你能帮我吗?你知道吗


Tags: 方法seleniumfindclassbeautifulsoupfindvaluefindclass
3条回答

如果您需要执行一些任务,如单击按钮,请使用seleniumapi。你知道吗

如果需要从HTML代码中提取数据,应该使用beautifulsoup。你知道吗

您可以使用selenium获取HTML代码,然后使用BeautifulSoup对其进行解析:

html_source = browser.page_source
soup = BeautifulSoup(html_source)

这就是你要找的:

elements = []
classes = driver.find_elements_by_class(className)
for c in classes:
    l = c.find_elements_by_tag_name(tagName)
    if l:
        elements.extent(l)

等于

soup.findAll("div", { "class" : className})

您只需将BeautifulSoup代码转换为功能强大的CSS选择器表达式,然后使用^{}

# equivalent to : soup.find("div", class_="list-container")
driver.find_element_by_css("div.list-container")

# equivalent to : soup.find("div", class_="list-container").find_all("a")
driver.find_elements_by_css("div.list-container a")

相关问题 更多 >