目标网页:https://www.lagou.com/zhaopin/ 完成重庆市计算机行业工作的查找,月薪5k-10k 要求调用到的方法: 1.下拉菜单的选择; 2.xpath定位,并使用最新的find_element语法
时间: 2024-06-08 14:07:31 浏览: 118
Python网页抓取/拉古/LaGou.py
以下是使用Python和Selenium实现的代码:
```python
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
# 设置Chrome浏览器的驱动路径
driver_path = "/path/to/chromedriver"
driver = webdriver.Chrome(executable_path=driver_path)
# 访问目标网页
url = "https://www.lagou.com/zhaopin/"
driver.get(url)
# 等待下拉菜单可见并选择地点为重庆
location_btn = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//div[@class='tab-box']//a[contains(text(),'地点')]"))
)
location_btn.click()
chongqing_loc = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//div[@class='area-box']//a[contains(text(),'重庆')]"))
)
chongqing_loc.click()
# 输入搜索关键词并提交搜索
search_input = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//div[@class='search_input']/input"))
)
search_input.send_keys("计算机")
search_submit = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//div[@class='search_button']/input"))
)
search_submit.click()
# 选择月薪范围为5k-10k
salary_range_btn = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//div[@class='tab-box']//a[contains(text(),'薪资')]"))
)
salary_range_btn.click()
salary_range = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//div[@class='salary-box']//li[contains(text(),'5k-10k')]"))
)
salary_range.click()
# 获取搜索结果
job_list = WebDriverWait(driver, 10).until(
EC.visibility_of_all_elements_located((By.XPATH, "//ul[@class='item_con_list']/li"))
)
for job in job_list:
title_element = job.find_element(By.XPATH, "./div[@class='list_item_top']/div[@class='position']//h3/a")
title = title_element.text
company_element = job.find_element(By.XPATH, "./div[@class='list_item_top']/div[@class='company']//a")
company = company_element.text
salary_element = job.find_element(By.XPATH, "./div[@class='list_item_top']/div[@class='position']/div[@class='p_bot']/div[@class='li_b_l']/span")
salary = salary_element.text
print(f"职位:{title},公司:{company},月薪:{salary}")
# 关闭浏览器
driver.quit()
```
代码执行后会在控制台输出搜索结果的职位、公司和月薪。注意要将代码中的`/path/to/chromedriver`替换为你本地Chrome浏览器驱动的路径。
阅读全文