首页 > 解决方案 > 如何从 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 标记中所有链接的列表...

标签: pythonseleniumweb-scrapingsrc

解决方案


如前所述,没有任何类具有确切的类属性。但是有一个类确实包含它。所以你实际上想要找到包含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;*,*

推荐阅读