Python進行拉勾網數據爬取框架與思路

爬取內容

用交互式的方式輸入想查詢的職位並循環輸出職位簡要信息,爲了方便閱讀,本文使用到的變量並非以目標變量的英文來命名,而是爲了方便而取的變量名,各位大牛請諒解。(因貴網站爬取必定量數據後須要登錄,因此只能爬取前幾頁內容,本文僅供新手參考)html

須要用到的第三方庫

from selenium import webdriver import time 

設置Google驅動的路徑

selenium須要用到的谷歌瀏覽器驅動driver可在官方自行下載官方驅動下載網址,找到與本身谷歌瀏覽器對應的前兩位數字版本後,下載好後解壓,複製於當前使用的編輯器python的目錄下的Scripts文件夾中,並在環境變量的系統變量中添加設置此路徑便可。
例如:python

lj = r'C:\Users\1111\AppData\Local\Programs\Python\Python36\Scripts' 

爲了方便理解,如上的命名不規範,請讀者諒解。web

用selenium打開瀏覽器驅動

def turnChrome(): print('正在打開瀏覽器') lj = r'C:\Users\1111\AppData\Local\Programs\Python\Python36\Scripts'#路徑變量lj driver = webdriver.Chrome(executable_path=(lj+"\chromedriver")) #啓動瀏覽器 time.sleep(1) return driver 

目標爬取網站

def driverturn(driver): driver.get('https://www.lagou.com') 

 

排除干擾項(*)

(1)讀者可先打開目標爬取網站,使用右鍵檢查的copy中的Xpath路徑截取下目標量,如打開網頁後一步一步點擊輸入框並查詢,然後點擊肯定等,詳情可理解以下:sql

driver.find_element_by_xpath('//*[@id="changeCityBox"]/p[1]/a').click() #點擊'全國'按鈕 driver.find_element_by_xpath('//*[@id="search_input"]').send_keys(jobname) #輸入內容 time.sleep(1) driver.find_element_by_xpath('//*[@id="search_button"]').click() #查詢 

(2)然後是干擾項:
例如網站有廣告紅包須要點擊等,對你進行了阻礙沒法讓爬蟲運行,那麼能夠以下解決:chrome

try: driver.find_element_by_xpath("/html/body/div[9]/div/div[2]").click() #點掉紅包 except: pass 

由於爬取時查詢的數據不一樣,有些時候會出現干擾項而有些時候並不會,因此當你加入了干擾項排除後如不加入try except模塊則會報錯,如加入後邏輯就是干擾項出現時就自動進行取消,如沒有干擾項則跳過,此外,貴網站的廣告紅包xpath常常會變化,例如:
一週前是:npm

driver.find_element_by_xpath("/html/body/div[8]/div/div[2]").click() 

一週後則是:瀏覽器

driver.find_element_by_xpath("/html/body/div[9]/div/div[2]").click() 

body中的div標籤不一樣致使爬蟲沒法進行,讀者第一次如遇到沒法進行爬蟲可先將紅包的xpath從新進行爬取,再繼續嘗試。
還有一個是彈框詢問你是否感興趣的干擾項也可如上進行排除,例如:網絡

try: driver.find_element_by_xpath('//*[@id="main_container"]/div[1]/div[4]/div/div/div[2]/button').click() except: pass 

整理後進行放入方法

def ganrao(driver,jobname): #進行干擾項排除 driver.find_element_by_xpath('//*[@id="changeCityBox"]/p[1]/a').click() #點擊全國 driver.find_element_by_xpath('//*[@id="search_input"]').send_keys(jobname) #輸入 time.sleep(1) driver.find_element_by_xpath('//*[@id="search_button"]').click() #查詢 try: driver.find_element_by_xpath("/html/body/div[9]/div/div[2]").click() #點掉紅包 except: pass try: driver.find_element_by_xpath('//*[@id="main_container"]/div[1]/div[4]/div/div/div[2]/button').click() except: pass 

進行爬取

自行打開查詢後,可看到一整頁的職位信息,而後選其中一個進行右鍵xpath索取,而後小編髮現一頁有16個數據,可用for循環進行逐個爬取,以下:app

def Paqu(driver,yeshu): CPname = [] #公司名 Jobname = [] #職位名 XZJY = [] #薪資經驗 Address = [] #地址 for num in range(yeshu): time.sleep(1) for i in range(1, 16): # //*[@id="s_position_list"]/ul/li[1]/div[1]/div[2]/div[1]/a a = driver.find_element_by_xpath('//*[@id="s_position_list"]/ul/li[{}]/div[1]/div[2]/div[1]/a'.format(i)) b = driver.find_element_by_xpath('//*[@id="s_position_list"]/ul/li[{}]/div[1]/div[1]/div[1]/a/h3'.format(i)) c = driver.find_element_by_xpath('//*[@id="s_position_list"]/ul/li[{}]/div[1]/div[1]/div[2]/div'.format(i)) d = driver.find_element_by_xpath('//*[@id="s_position_list"]/ul/li[{}]/div[1]/div[1]/div[1]/a/span/em'.format(i)) CPname.append(a.text) Jobname.append(b.text) XZJY.append(c.text) Address.append(d.text) if yeshu-num == 1: print('爬取結束') else: driver.find_element_by_class_name("pager_next").click() driver.quit() return CPname,Jobname,XZJY,Address 

如上中式英語的變量方便理解,若有不適請見諒。編輯器

存入文本

拿到四個數據的列表後,可進行數據清洗的存儲工做了:

def Save(CPname,Jobname,XZJY,Address): with open('職位原始記錄.txt','w+',encoding='utf-8') as f: #l1 = ['ID','公司名','職位名','薪資','經驗','學歷','地址'] for i in range(len(CPname)): k = str(XZJY[i]).replace(" / "," ").split(' ') l2 = [str(i+1),CPname[i],Jobname[i],k[0],k[1],k[2],Address[i]] #dabao = str(dict(zip(l1,l2))).replace("'",'"') f.write(str(l2).replace('[','').replace(']','')+'\n') print(l2) 

能夠清洗成本身想要的樣子,這個讀者自行編寫。

所有源碼

#coding=utf-8 from selenium import webdriver import time import sqlite3 def turnChrome(): print('正在打開瀏覽器') lj = r'C:\Users\38376\AppData\Local\Programs\Python\Python36\Scripts' driver = webdriver.Chrome(executable_path=(lj+"\chromedriver")) #啓動瀏覽器 time.sleep(1) return driver def driverturn(driver): driver.get('https://www.lagou.com') def ganrao(driver,jobname): #進行干擾項排除 driver.find_element_by_xpath('//*[@id="changeCityBox"]/p[1]/a').click() #點擊全國 driver.find_element_by_xpath('//*[@id="search_input"]').send_keys(jobname) #輸入 time.sleep(1) driver.find_element_by_xpath('//*[@id="search_button"]').click() #查詢 try: driver.find_element_by_xpath("/html/body/div[9]/div/div[2]").click() #點掉紅包 except: pass try: driver.find_element_by_xpath('//*[@id="main_container"]/div[1]/div[4]/div/div/div[2]/button').click() except: pass def Paqu(driver,yeshu): CPname = [] Jobname = [] XZJY = [] Address = [] for num in range(yeshu): time.sleep(1) for i in range(1, 16): # //*[@id="s_position_list"]/ul/li[1]/div[1]/div[2]/div[1]/a a = driver.find_element_by_xpath('//*[@id="s_position_list"]/ul/li[{}]/div[1]/div[2]/div[1]/a'.format(i)) b = driver.find_element_by_xpath('//*[@id="s_position_list"]/ul/li[{}]/div[1]/div[1]/div[1]/a/h3'.format(i)) c = driver.find_element_by_xpath('//*[@id="s_position_list"]/ul/li[{}]/div[1]/div[1]/div[2]/div'.format(i)) d = driver.find_element_by_xpath('//*[@id="s_position_list"]/ul/li[{}]/div[1]/div[1]/div[1]/a/span/em'.format(i)) CPname.append(a.text) Jobname.append(b.text) XZJY.append(c.text) Address.append(d.text) if yeshu-num == 1: print('爬取結束') else: driver.find_element_by_class_name("pager_next").click() driver.quit() return CPname,Jobname,XZJY,Address def Save(CPname,Jobname,XZJY,Address): with open('職位原始記錄.txt','w+',encoding='utf-8') as f: #l1 = ['ID','公司名','職位名','薪資','經驗','學歷','地址'] for i in range(len(CPname)): k = str(XZJY[i]).replace(" / "," ").split(' ') l2 = [str(i+1),CPname[i],Jobname[i],k[0],k[1],k[2],Address[i]] #dabao = str(dict(zip(l1,l2))).replace("'",'"') f.write(str(l2).replace('[','').replace(']','')+'\n') print(l2) if __name__=='__main__': jobname = input('請輸入你想查詢的職位名:') yeshu = int(input('你想查詢多少頁,最高30頁:')) driver = turnChrome() driverturn(driver) ganrao(driver,jobname) CPname,Jobname,XZJY,Address = Paqu(driver,yeshu) Save(CPname,Jobname,XZJY,Address) 

本文的文字及圖片來源於網絡,僅供學習、交流使用,不具備任何商業用途,若有問題請及時聯繫咱們以做處理想要獲取更多Python學習資料能夠加QQ:2955637827私聊或加Q羣630390733你們一塊兒來學習討論吧!

相關文章
相關標籤/搜索