【问题标题】:Trying to grab location of job postings on website using python尝试使用 python 在网站上获取职位发布的位置
【发布时间】:2020-08-19 09:18:01
【问题描述】:

我正在尝试获取每个作业的位置标签,以根据位置过滤它们,因为此选项不可用 Seek Work From Home 并且一直在使用 python 和 selenium。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import pandas as pd

driver = webdriver.Chrome("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe")

driver.get("https://www.seek.com.au/jobs?where=Work%20from%20home")
assert "SEEK" in driver.title

location = WebDriverWait(driver, 25).until(EC.visibility_of_all_elements_located((By.XPATH,
                                                                                   '("//*[@id=""app""]/div/div'
                                                                                   '/div[4]/div/div[3]/section'
                                                                                   '/div[2]/div/div[2]/div["'''
                                                                                   '"1]/div/div[2]/div/div[1]/'
                                                                                   'div[2]/article/div[1]/span'
                                                                                   '[2]/span/strong/span/span"'
                                                                                   ')')))``

WebDriverWait 在尝试查找位置为文本的元素时似乎超时(尽管尝试了疯狂的等待时间)

Traceback (most recent call last):
  File "C:/Users/meagl/Desktop/Python/grabjobs/grabjobs.py", line 13, in <module>
    location = WebDriverWait(driver, 25).until(EC.visibility_of_all_elements_located((By.XPATH,
  File "C:\Users\meagl\anaconda3\envs\Python\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

我使用的 XPATH 位于最顶端。 (目前显示为悉尼) 我的下一步是什么?

【问题讨论】:

    标签: python selenium-webdriver


    【解决方案1】:

    您的 xPath 中似乎存在问题。正如我使用下面的代码,它打印了页面上所有 20 个作业的所有位置:

    driver.get("https://www.seek.com.au/jobs?where=Work%20from%20home")
    assert "SEEK" in driver.title
    location = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[contains(text(),'location:')]")))
    for loc in location:
        print(loc.text)
    

    输出

    注意:如果您只想获取城市名称,可以使用字符串。

    【讨论】:

      【解决方案2】:

      每当您进行此类操作时,都应仔细选择定位器,而此处使用的 xpath 不起作用。以//*[text()='location:'] 的方式使用xpath 将解决您的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-14
        • 1970-01-01
        • 1970-01-01
        • 2013-11-16
        • 2018-10-14
        相关资源
        最近更新 更多