selenium - 是否可以一次找到 html 标签的所有父标签以进行网络抓取?
问题描述
我是网络抓取的新手,我想知道不是找到所需的标签并回溯其每个父标签,还有更好的方法吗?我在哪里可以 body
一次将所有标签从 到我们想要的标签?
解决方案
如果我理解你的问题,使用 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")
希望这会有所帮助,欢迎来到网络自动化,这是一个有趣的世界!
推荐阅读
- c++ - GLib-GIO-CRITICAL **: g_application_run: assertion 'acquired_context' failed 是什么意思
- python - cv2.IMREAD_GRAYSCALE 如何将 16 位图像转换为 8 位(OpenCV python)?
- plotly - python3中plotly对象的类型提示是什么
- apache-kafka - 即使在 kafka 机器重新启动后,如何保留 kafka 保留字节和 kafka 保留段
- mysql - 缓慢的 MySQL 查询,第 1 行“使用 where;使用临时;使用文件排序。” 似乎是问题
- java - 格式化负双精度时,DecimalFormat 返回奇怪的字符
- perl - 需要使用 Perl 设置和读取 cookie
- nancy - Topshelf:在 Topshelf 4.2.1 中使用 NancyFX 的问题
- python - Python idle 不允许使用通过子进程调用在 shell 中安装 Telepot
- java - 我的输出中出现随机字符,不确定它们来自哪里