首页 > 解决方案 > 是否可以一次找到 html 标签的所有父标签以进行网络抓取?

问题描述

我是网络抓取的新手,我想知道不是找到所需的标签并回溯其每个父标签,还有更好的方法吗?我在哪里可以 body一次将所有标签从 到我们想要的标签?

标签: seleniumweb-scrapingbeautifulsoup

解决方案


如果我理解你的问题,使用 beautifulsoup 是最好的方法。(在蟒蛇中)

from bs4 import BeautifulSoup

//parse html using BeautifulSoup
doc = BeautifulSoup("html link", features="lxml")//add .getText() for no tags

//loop through all lines in body (including tags)
for d in doc:
     print(d)

然后,您可以将 print 语句替换为 d.find("tag") 以查找标签的位置并获取信息。

我认为更好的方法是只使用 seleniums find_element_by_xpath

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("link")

element = driver.find_element_by_xpath("your xpath")

希望这会有所帮助,欢迎来到网络自动化,这是一个有趣的世界!


推荐阅读