如何解决尝试使用python获取网站上职位发布的位置
我正在尝试获取每个作业的位置标签,以根据位置过滤它们,因为该选项在 Seek Work From Home,并且一直在将python与硒结合使用。
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,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是最顶层的XPATH。 (当前显示为悉尼) 我的下一步是什么?
解决方法
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)
输出
注意:如果您只想获取城市名称,则可以使用字符串来玩。
,每当您执行定位器这种操作时,都应仔细选择,并且此处使用的xpath不起作用。以//*[text()='location:']
的方式使用xpath将解决您的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。