gpt4 book ai didi

python - 在抓取中查找网络图像的位置

转载 作者:太空宇宙 更新时间:2023-11-04 04:13:31 24 4
gpt4 key购买 nike

有没有办法在网站上找到图像的位置(即左偏移和上偏移)?可以通过网络图像抓取来完成吗?我使用的语言是 Python。

最佳答案

如果你定义的position是图片的x-y像素坐标(其实可以是任意元素)。您可以使用 Selenium 轻松地做到这一点:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://datafireball.com")
# I am using Chrome Developer Tool to get the Xpath, you might want to use your own code.
pic = driver.find_element_by_xpath('//*[@id="post-174"]/div/p[5]/a/img')
position = pic.location
print(position)

输出是:

{'y': 727, 'x': 493}

这只是一张图片,向您展示了 x 坐标是正确的。同样适用于 y。注意:当通过Python代码打开浏览器时,用户可能并没有真正直接看到图片。但是代码仍然会捕获位置。它还取决于您的浏览器的配置文件。因此,您可以为浏览器设置默认窗口大小、缩放比例等以获得可重现的结果。

有关 Selenium 设置的更多信息,请单击 here .

enter image description here

关于python - 在抓取中查找网络图像的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20315715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com