为了账号安全,请及时绑定邮箱和手机立即绑定

我在通过 selenium python 抓取内容时遇到错误

我在通过 selenium python 抓取内容时遇到错误

眼眸繁星 2023-03-08 16:14:56
我正在通过 selenium 在https://www.indeed.ae/jobs-in-dubai上抓取工作结果的标题。我认为 .text 不起作用。我正在通过转到主网站的 selenium 运行代码,输入选择性关键字,然后从结果中删除所有标题。但是我收到错误,我该如何解决这个错误这是我的代码import timefrom selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.keys import KeysPath = "C:\Program Files (x86)\chromedriver.exe"driver = webdriver.Chrome(Path)driver.get("https://indeed.ae/")print(driver.title)search = driver.find_element_by_name("l")search.send_keys("Dubai")search.send_keys(Keys.RETURN)try:    td = WebDriverWait(driver, 10).until(        EC.presence_of_element_located((By.ID, "resultsCol"))    )    divs = td.find_elements_by_tag_name("div")    for div in divs:        header = div.find_element_by_class_name("title")        print(header)finally:    driver.quit()driver.quit()我收到以下错误Job Search | IndeedTraceback (most recent call last):  File "C:/Users/hp/Desktop/python projects/selenium-pycharm/selenium-bot.py", line 24, in <module>    header = div.find_element_by_class_name("title")  File "C:\Users\hp\Desktop\python projects\selenium-pycharm\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 398, in find_element_by_class_name    return self.find_element(by=By.CLASS_NAME, value=name)  File "C:\Users\hp\Desktop\python projects\selenium-pycharm\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 659, in find_element    {"using": by, "value": value})['value']  File "C:\Users\hp\Desktop\python projects\selenium-pycharm\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute    return self._parent.execute(command, params)Process finished with exit code 1提前致谢
查看完整描述

1 回答

?
largeQ

TA贡献2039条经验 获得超7个赞

您找不到标题,因为您从 resultsCol 中获取了所有的 div。这意味着有些 div 有标题,有些则没有。


尝试这个 :


try:

    td = WebDriverWait(driver, 10).until(

        EC.presence_of_element_located((By.ID, "resultsCol"))

    )

    divs = td.find_elements_by_tag_name("div")

    #print(divs)

    for div in divs:

        try:

            header = div.find_element_by_class_name("title")

            print(header.text)

        except:

            continue

finally:

    driver.quit()


driver.quit()

将标题作为输出:


Receptionist

Administrative Assistant/ Document Controller

RECEPTIONIST

ADMIN OFFICER IN UAE

Data Entry Assistant (Fresh Graduate)

Receptionist

Replenishment Associate - Light Household - Hypermarket

DOCUMENT CONTROLLER

School Administrative Assistant - Dubai

ACCOUNTANT


查看完整回答
反对 回复 2023-03-08
  • 1 回答
  • 0 关注
  • 90 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信