python - 如何从 Zomato 订单页面获取 img (src) 链接?
问题描述
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
PATH = "/Users/macop/Tools/ChromeDriver/chromedriver"
driver = webdriver.Chrome(PATH)
driver.get("https://www.zomato.com/ncr/dominos-pizza-connaught-place-new-delhi/order")
time.sleep(5)
link = driver.find_element_by_class_name("hppEfq").get_attribute('src')
print(link)
for name in link:
print(name.text)
driver.quit()
我得到的最后一个错误:
Message: no such element: Unable to locate element: {"method":"css selector","selector":".hppEfq"}
正在查找该页面属性 src 的 img 标记中所有链接的列表...
解决方案
如前所述,没有任何类具有确切的类属性。但是有一个类确实包含它。所以你实际上想要找到包含hppEfq
.
另外,由于您只说要查找第一个元素,因此不确定您要遍历什么。最后,要渲染该元素,您需要向下滚动。
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.common.by import By
PATH = "C:/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.zomato.com/ncr/dominos-pizza-connaught-place-new-delhi/order")
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.PAGE_DOWN)
time.sleep(5)
link = driver.find_element_by_css_selector("img[class*='hppEfq']").get_attribute('src')
print(link)
driver.quit()
输出:
https://b.zmtcdn.com/data/dish_photos/a3d/7ca006ec8907c2ae13fd006cf0853a3d.png?output-format=webp&fit=around|130:130&crop=130:130;*,*
推荐阅读
- spring-boot - 如何在 RestController 之前拦截一个 RequestBody 并从另一个微服务处理一些业务规则?
- swift - SceneKit 和 ARKit 中的 simdTransform 是什么?
- excel - Excel:如何多次在表格中找到最接近的数字
- apache - 限制对 Apache 2.4 虚拟主机 (WAMP) 的访问
- django - 多个 Django 模型之间的大量数据传输
- node.js - db.getCollectionInfos 不是函数
- objective-c - NSFileManager isWritableFileAtPath false
- tensorflow-datasets - Tensorflow 数据集交错读取图像文件路径
- arrays - R:根据列名将 JAGS 输出矩阵转换为数组
- c# - 如何在 C# 中正确检查 HttpResponseMessage 的 http 状态代码?