Python+Selenium=None导致href?

2024-10-01 00:25:57 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我对selenium没有太多的经验,我尝试使用youtube+一些论坛帖子来创建一个剪贴代码,因为我是一名摄影师,我与该领域的其他人一起工作,如果能得到所有摄影师的名单,那就太棒了,让我来展示代码,所以基本上现在,我只对这里的摄影师姓名感兴趣(稍后我需要学习selenium中的分页,但首先我必须通过这个问题):

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import sys


PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
bloom ="https://www.photographer.org/?geodir_search=1&stype=gd_place&sgd_placecategory%5B0%5D=8&s=+&snear&sgeo_lat&sgeo_lon"
driver.get(bloom)

eList = driver.find_elements_by_class_name('geodir-entry-title')


hrefList = []
for a in eList:
    hrefList.append(a.get_attribute('href'))

for href in hrefList:
    print(href)

结果是没有,没有,没有,没有,等等。。。 它应该是“Tsukimi摄影”、“Cherry Logan摄影”等

有什么提示我可能会被卡住吗

谢谢


Tags: path代码fromimportgetdriverseleniumhref
1条回答
网友
1楼 · 发布于 2024-10-01 00:25:57

查看html,我可以看到类为geodir-entry-title的元素没有href属性。因此,None上的输出是预期的。href的预期值类似于“https://www.photographer.org/photographers/tsukimi-photography/"

<h3 class="geodir-entry-title">
   <a href="https://www.photographer.org/photographers/tsukimi-photography/" title="Tsukimi Photography">
      Tsukimi Photography
   </a>
</h3>

这可以像下面这样做

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
import sys

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
bloom ="https://www.photographer.org/?geodir_search=1&stype=gd_place&sgd_placecategory%5B0%5D=8&s=+&snear&sgeo_lat&sgeo_lon"
driver.get(bloom)

eList = WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located(
        (By.XPATH, "//h3[@class='geodir-entry-title']//a")))


hrefList = []
for a in eList:
    hrefList.append(a.get_attribute('href'))

for href in hrefList:
    print(href)

相关问题 更多 >