在這裏,Selenium結合lxml來獲取51job招聘網站西安地區自動化測試招聘的公司名稱,薪資範圍,職位要求和招聘的Title,具體實現的思路是訪問爲:
- www.51job.com後,在搜索輸入框輸入搜索的關鍵字“自動化測試”,點擊搜索按鈕
- 獲取第一頁(列表頁顯示的信息包含了各個公司的招聘信息)的源碼,獲取源碼後
- 使用lxml來對源碼進行解析,獲取每個公司招聘詳情信息的URL,也就是鏈接地址
- 然後點擊跳轉到每個公司招聘的詳情頁面,再獲取詳情頁面的源碼,再使用lxml進行解析,獲取到具體招聘單位的公司名稱,招聘Title,職位要求,薪資範圍
如上是實現的思路,具體見實現的代碼。這裏Selenium版本是3.13版本,Chrome瀏覽器版本號是68,Python使用的版本是Python3.6的版本。
首先導入需要的庫,見源碼:
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from lxml import etree import requests import time as t import re import csv
然後定義Job類,編寫構造函數,以及打開www.51job.com後,關鍵字搜索,獲取到第一頁的列表源碼並且解析,獲取每個公司招聘的詳情頁面的鏈接地址,見實現的源碼:
class Job(object): '''selenium結合網絡爬蟲獲取5job西安地區招聘自動化測試工程師的薪資和要求''' def __init__(self): self.info=[] self.driver=webdriver.Chrome() self.url='http://www.51job.com' def parse_list_page(self): self.driver.implicitly_wait(30) self.driver.maximize_window self.driver.get(self.url) #輸入搜索的關鍵字 self.driver.find_element_by_id('kwdselectid').send_keys('自動化測試工程師') #點擊搜索按鈕 self.driver.find_element_by_xpath('/html/body/div[3]/div/div[1]/div/button').click() source=self.driver.page_source #對搜索後的源碼進行解析 html=etree.HTML(source) #獲取所有招聘信息的詳情URL links=html.xpath('//div[@class="dw_table"]/div[@class="el"]//span[not(@class="t2")]/a/@href') for link in links: t.sleep(3) #對詳情頁面的數據進行分析 self.request_detail_page(link)
然後編寫方法request_detail_page解析每個公司招聘詳情頁面的數據,這地方同時涉及到多窗口的操作(這部分的知識不做解釋,如有疑問,可查看本人寫的Selenium的文章),在方法requets_detail_page中,點擊每個詳情的鏈接地址跳轉到詳情頁,見源碼:
def request_detail_page(self,url): '''在列表頁點擊跳轉到詳情頁面''' #切換窗口 self.driver.execute_script("window.open('%s')"%url) #切換到詳情頁 self.driver.switch_to_window(self.driver.window_handles[1]) #獲取詳情頁的頁面資源 source=self.driver.page_source #解析詳情頁面後關閉頁面 self.driver.close() #繼續切換到列表頁 self.driver.switch_to_window(self.driver.window_handles[0]) self.parse_detail_page(source)
然後是方法parse_detail_page,該方法是指到詳情頁面後,獲取詳情的源碼,對源碼進行解析並且獲取到招聘單位的公司名稱,招聘Title,職位要求,薪資範圍,並且把這些數據放到一個字典中,見parse_detail_page方法的源碼:
def parse_detail_page(self,source): '''對招聘詳情頁的數據進行解析''' #對詳情頁的數據進行分析 html=etree.HTML(source) #獲取公司基本信息 infos=html.xpath('//div[@class="cn"]') for info in infos: #獲取公司名稱 companyName=info.xpath('//p[@class="cname"]/a/@title')[0].strip() #獲取招聘title title=info.xpath('./h1/text()')[0].strip() #獲取招聘薪資 salary=info.xpath('./strong/text()')[0] #獲取職位信息 position=html.xpath('//div[@class="bmsg job_msg inbox"]/p/text()') position=''.join(position).strip() jobInfo={ '公司名稱':companyName, '招聘職位':title, '薪資範圍':salary, '職位信息':position } print(jobInfo)
最後寫一個方法run,在該方法裏面調用方法parse_list_page的方法就可以了,見完整的源碼:
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from lxml import etree import requests import time as t import re import csv class Job(object): '''selenium結合網絡爬蟲獲取5job西安地區招聘自動化測試工程師的薪資和要求''' def __init__(self): self.info=[] self.driver=webdriver.Chrome() self.url='http://www.51job.com' def run(self): self.parse_list_page() self.driver.quit() def parse_list_page(self): self.driver.implicitly_wait(30) self.driver.maximize_window self.driver.get(self.url) #輸入搜索的關鍵字 self.driver.find_element_by_id('kwdselectid').send_keys('自動化測試工程師') #點擊搜索按鈕 self.driver.find_element_by_xpath('/html/body/div[3]/div/div[1]/div/button').click() source=self.driver.page_source #對搜索後的源碼進行解析 html=etree.HTML(source) #獲取所有招聘信息的詳情URL links=html.xpath('//div[@class="dw_table"]/div[@class="el"]//span[not(@class="t2")]/a/@href') for link in links: t.sleep(3) #對詳情頁面的數據進行分析 self.request_detail_page(link) def request_detail_page(self,url): '''在列表頁點擊跳轉到詳情頁面''' #切換窗口 self.driver.execute_script("window.open('%s')"%url) #切換到詳情頁 self.driver.switch_to_window(self.driver.window_handles[1]) #獲取詳情頁的頁面資源 source=self.driver.page_source #解析詳情頁面後關閉頁面 self.driver.close() #繼續切換到列表頁 self.driver.switch_to_window(self.driver.window_handles[0]) self.parse_detail_page(source) def parse_detail_page(self,source): '''對招聘詳情頁的數據進行解析''' #對詳情頁的數據進行分析 html=etree.HTML(source) #獲取公司基本信息 infos=html.xpath('//div[@class="cn"]') for info in infos: #獲取公司名稱 companyName=info.xpath('//p[@class="cname"]/a/@title')[0].strip() #獲取招聘title title=info.xpath('./h1/text()')[0].strip() #獲取招聘薪資 salary=info.xpath('./strong/text()')[0] #獲取職位信息 position=html.xpath('//div[@class="bmsg job_msg inbox"]/p/text()') position=''.join(position).strip() jobInfo={ '公司名稱':companyName, '招聘職位':title, '薪資範圍':salary, '職位信息':position } print(jobInfo) if __name__ == '__main__': job=Job() job.run()
執行如上的代碼後,就會獲取到最初設計的數據,這裏對這些數據就不顯示了,實在是數據太多。後期可以對薪資範圍這部分進行數據分析。
在這裏,無涯提前祝大家國慶節快樂!!!