Python 结束刮削的最有效方法?
我有一个我从一个用例的回购协议中修改的函数。它使用webdriver刮取glassdoor职位发布数据。与问题相关-该函数使用一个参数来表示搜索项和要刮取的作业数。它成功地收集了数据,但是搜索词的某些区域包含的过账少于传递给函数调用的数量。因此,一旦没有更多的工作公告,我需要结束刮取,覆盖传递给函数的值。以下内容用于识别列表:Python 结束刮削的最有效方法?,python,selenium-webdriver,Python,Selenium Webdriver,我有一个我从一个用例的回购协议中修改的函数。它使用webdriver刮取glassdoor职位发布数据。与问题相关-该函数使用一个参数来表示搜索项和要刮取的作业数。它成功地收集了数据,但是搜索词的某些区域包含的过账少于传递给函数调用的数量。因此,一旦没有更多的工作公告,我需要结束刮取,覆盖传递给函数的值。以下内容用于识别列表: job_buttons = driver.find_elements_by_class_name("jl") 最有效的方法是什么?我曾考虑过测试每个
job_buttons = driver.find_elements_by_class_name("jl")
最有效的方法是什么?我曾考虑过测试每个帖子上是否存在一个可识别的web元素,但我不确定如何将其作为一个条件来实现
以下是完整的功能定义:
def get_jobs(job_name, region_id, num_jobs, verbose, path, slp_time):
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path=path, options=options)
driver.set_window_size(1120, 1000)
url = "https://www.glassdoor.com/Job/jobs.htm?suggestCount=0&suggestChosen=false&clickSource=searchBtn&typedKeyword="+job_name+"&sc.keyword=data+analyst&locT=N&locId="+region_id+"&jobType="
driver.get(url)
jobs = []
while len(jobs) < num_jobs:
time.sleep(slp_time)
#Test for "Sign Up" prompt and remove
try:
driver.find_element_by_class_name("selected").click()
except ElementClickInterceptedException:
pass
time.sleep(.1)
try:
driver.find_element_by_css_selector('[alt="Close"]').click()
print(' x out worked')
except NoSuchElementException:
print(' x out failed (next page or missing)')
pass
job_buttons = driver.find_elements_by_class_name("jl")
for job_button in job_buttons:
print("Progress: {}".format("" + str(len(jobs)) + "/" + str(num_jobs)))
if len(jobs) >= num_jobs:
break
job_button.click()
#
time.sleep(1)
collected_successfully = False
while not collected_successfully:
try:
company_name = driver.find_element_by_xpath('.//div[@class="employerName"]').text
location = driver.find_element_by_xpath('.//div[@class="location"]').text
job_title = driver.find_element_by_xpath('.//div[contains(@class, "title")]').text
job_description = driver.find_element_by_xpath('.//div[@class="jobDescriptionContent desc"]').text
collected_successfully = True
except:
time.sleep(5)
try:
salary_estimate = driver.find_element_by_xpath('.//span[@class="gray salary"]').text
except NoSuchElementException:
salary_estimate = -1
try:
rating = driver.find_element_by_xpath('.//span[@class="rating"]').text
except NoSuchElementException:
rating = -1
#Debugging printouts
if verbose:
print("Job Title: {}".format(job_title))
print("Salary Estimate: {}".format(salary_estimate))
print("Job Description: {}".format(job_description[:500]))
print("Rating: {}".format(rating))
print("Company Name: {}".format(company_name))
print("Location: {}".format(location))
#Go to Company tab...
#clicking on:
#<div class="tab" data-tab-type="overview"><span>Company</span></div>
try:
driver.find_element_by_xpath('.//div[@class="tab" and @data-tab-type="overview"]').click()
try:
#<div class="infoEntity">
# <label>Headquarters</label>
# <span class="value">San Francisco, CA</span>
#</div>
headquarters = driver.find_element_by_xpath('.//div[@class="infoEntity"]//label[text()="Headquarters"]//following-sibling::*').text
except NoSuchElementException:
headquarters = -1
try:
size = driver.find_element_by_xpath('.//div[@class="infoEntity"]//label[text()="Size"]//following-sibling::*').text
except NoSuchElementException:
size = -1
try:
founded = driver.find_element_by_xpath('.//div[@class="infoEntity"]//label[text()="Founded"]//following-sibling::*').text
except NoSuchElementException:
founded = -1
try:
type_of_ownership = driver.find_element_by_xpath('.//div[@class="infoEntity"]//label[text()="Type"]//following-sibling::*').text
except NoSuchElementException:
type_of_ownership = -1
try:
industry = driver.find_element_by_xpath('.//div[@class="infoEntity"]//label[text()="Industry"]//following-sibling::*').text
except NoSuchElementException:
industry = -1
try:
sector = driver.find_element_by_xpath('.//div[@class="infoEntity"]//label[text()="Sector"]//following-sibling::*').text
except NoSuchElementException:
sector = -1
try:
revenue = driver.find_element_by_xpath('.//div[@class="infoEntity"]//label[text()="Revenue"]//following-sibling::*').text
except NoSuchElementException:
revenue = -1
try:
competitors = driver.find_element_by_xpath('.//div[@class="infoEntity"]//label[text()="Competitors"]//following-sibling::*').text
except NoSuchElementException:
competitors = -1
except NoSuchElementException: #Rarely, some job postings do not have the "Company" tab.
headquarters = -1
size = -1
founded = -1
type_of_ownership = -1
industry = -1
sector = -1
revenue = -1
competitors = -1
if verbose:
print("Headquarters: {}".format(headquarters))
print("Size: {}".format(size))
print("Founded: {}".format(founded))
print("Type of Ownership: {}".format(type_of_ownership))
print("Industry: {}".format(industry))
print("Sector: {}".format(sector))
print("Revenue: {}".format(revenue))
print("Competitors: {}".format(competitors))
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
jobs.append({"Job Title" : job_title,
"Salary Estimate" : salary_estimate,
"Job Description" : job_description,
"Rating" : rating,
"Company Name" : company_name,
"Location" : location,
"Headquarters" : headquarters,
"Size" : size,
"Founded" : founded,
"Type of ownership" : type_of_ownership,
"Industry" : industry,
"Sector" : sector,
"Revenue" : revenue,
"Competitors" : competitors})
#add job to jobs
except ElementClickInterceptedException: # error fix
continue
return pd.DataFrame(jobs) #dict -> df
def get_作业(作业名称、区域id、数量作业、详细信息、路径、slp_时间):
options=webdriver.ChromeOptions()
driver=webdriver.Chrome(可执行文件路径=路径,选项=选项)
驱动程序。设置窗口大小(11201000)
url=”https://www.glassdoor.com/Job/jobs.htm?suggestCount=0&suggestChosen=false&clickSource=searchBtn&typedKeyword=“+job\u name+”&sc.keyword=data+analyst&locT=N&locId=“+region\u id+”&jobType=”
获取驱动程序(url)
工作=[]
而len(jobs)=num_jobs:
打破
作业按钮。单击()
#
时间。睡眠(1)
成功收集\u=错误
未成功收集\u时:
尝试:
公司名称=驱动程序。通过xpath('.//div[@class=“employerName”]')查找元素。文本
location=driver。通过xpath('.//div[@class=“location”]')查找元素。text
job_title=driver.find_element_by_xpath('.//div[contains(@class,“title”)]')。text
job_description=driver.find_element_by_xpath('.//div[@class=“jobsdescriptioncontent desc”]')。text
已成功收集\u=真
除:
时间。睡眠(5)
尝试:
salary_estimate=driver.by_xpath('.//span[@class=“gray salary”]')查找_元素。text
除无任何例外:
工资估计值=-1
尝试:
rating=driver。通过xpath('.//span[@class=“rating”]')查找元素。text
除无任何例外:
评级=-1
#调试打印输出
如果冗长:
打印(“职务:{}”。格式(职务)
打印(“工资估算:{}”。格式(工资估算))
打印(“职务说明:{}”。格式(职务说明[:500]))
打印(“评级:{}”。格式(评级))
打印(“公司名称:{}”。格式(公司名称))
打印(“位置:{}”。格式(位置))
#转到公司选项卡。。。
#点击:
#公司
尝试:
驱动程序。通过xpath('.//div[@class=“tab”和@data tab type=“overview”]”查找元素。单击()
尝试:
#
#总部
CA旧金山
#
总部=驱动程序。通过xpath('.//div[@class=“infoEntity”]//标签[text()=“总部”]//以下同级::*')查找元素。text
除无任何例外:
总部=-1
尝试:
size=driver。通过xpath('.//div[@class=“infoEntity”]//label[text()=“size”]//以下同级::*')查找元素。text
除无任何例外:
大小=-1
尝试:
founded=driver。通过xpath('.//div[@class=“infoEntity”]//label[text()=“founded”]//以下同级::*')查找元素。text
除无任何例外:
成立=-1
尝试:
type_of_ownership=driver.by_xpath('.//div[@class=“infoEntity”]//label[text()=“type”]//以下同级::*')查找_元素。text
除无任何例外:
所有权的类型=-1
尝试:
行业=驱动程序。通过xpath('.//div[@class=“infoEntity”]//标签[text()=“行业”]//以下同级::*')查找元素。text
exc