閑來無事掂名,想看看拉勾上關(guān)于的Python的招聘信息
于是。哟沫。饺蔑。爬下來唄
- 話不多說,直接開始
不對嗜诀,首先還是說一下主要使用到的技術(shù)棧猾警,這里我沒有使用requests庫孔祸,而是使用selenium爬的
- why ?
我喜歡唄~
- selenium爬蟲原理
其實原理也沒啥好說的发皿,和平時爬蟲的時候原理都是一樣的崔慧,就是模擬瀏覽器上網(wǎng)唄
- 分析:
其實,拉勾網(wǎng)是非常好爬的穴墅,首先進(jìn)入拉勾網(wǎng)(www.lagou.com)惶室,并搜索python 回車
為什么說拉勾網(wǎng)好爬,原因之一在圖中我已經(jīng)標(biāo)注出來了封救,就是不需要登錄,原因之二請見下文
在chrome瀏覽器按f12捣作,查看網(wǎng)頁源碼
此時會發(fā)現(xiàn)誉结,Network下什么都沒有,那是因為沒有請求券躁,此時惩坑,需要再此刷新頁面,就會有了
注意此時也拜,我們選擇箭頭所指的位置以舒,這里的數(shù)據(jù)是通過ajax發(fā)送過來的,這里就說一下蔓钟,拉勾網(wǎng)容易爬的原因之二,因為所有的數(shù)據(jù)在ajax里都能找到卵贱,只需要爬去這里的json數(shù)據(jù)就行了滥沫,但是我沒有這樣做,因為不屑于(裝)這么爬(B)键俱,開頭就說了這次爬蟲使用的是selenium兰绣,所以需要分析網(wǎng)頁的html。因此编振,這里的數(shù)據(jù)不是重點(diǎn)缀辩,我們看網(wǎng)頁
點(diǎn)擊Elements,查看網(wǎng)頁源碼踪央,再點(diǎn)擊左上角的小箭頭(我標(biāo)注的紅色小箭頭)所指的按鈕臀玄,再選中網(wǎng)頁中的元素,即可快速定位到該元素所在的源碼位置畅蹂,到此時镐牺,其實我們已經(jīng)找到想爬數(shù)據(jù)的所在位置了,在代碼中使用selenium自帶的xpath解析出來就可以了魁莉,接下來就是代碼實現(xiàn)了
- 話不多說直接上源碼:
import json
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
class Lagou(object):
def __init__(self, search_name):
self.start_url = "https://www.lagou.com/jobs/list_{}?px=default&city=%E5%8C%97%E4%BA%AC#filterBox"
self.search_name = search_name
self.chrome_options = Options()
self.chrome_options.add_argument('--headless')
self.driver = webdriver.Chrome(chrome_options=self.chrome_options)
self.content_header = ["positionName", "businessZones", "CreateTime", "companyShortName", "salary", "workYear"]
def get_content_list(self): # 提取數(shù)據(jù)
li_list = self.driver.find_elements_by_xpath('//li[contains(@class, "con_list_item")]')
content_list = []
for li in li_list:
item_list = []
item_list.append(li.find_element_by_tag_name("h3").text)
item_list.append(li.find_element_by_tag_name("em").text)
item_list.append(li.find_elements_by_xpath(".//span[@class='format-time']")[0].text)
item_list.append(li.find_elements_by_xpath(".//div[@class='company_name']")[0].text)
item_list.append(li.find_elements_by_xpath(".//span[@class='money']")[0].text)
item_list.append(li.find_elements_by_xpath(".//div[@class='li_b_l']")[0].text)
# print(item_list)
content_list.append(item_list)
# 下一頁
next_url = self.driver.find_elements_by_xpath("http://span[@class='pager_next ']")
next_url = next_url[0] if len(next_url) > 0 else None
return content_list, next_url
def save_content_list(self, content_list):
with open(self.search_name + "_data.csv", "a", encoding='utf-8') as f:
for content in content_list:
print(content)
for item in content:
if isinstance(item, str):
if '睬涧,' in item:
item.replace(",", "|")
f.write(item + ",")
f.write('\n')
def run(self):
# 發(fā)送請求
self.driver.get(self.start_url.format(self.search_name))
# 提取數(shù)據(jù)
content_list, next_url = self.get_content_list()
# 保存數(shù)據(jù)
self.save_content_list(content_list)
# 翻頁
while next_url is not None:
next_url.click()
time.sleep(6)
content_list, next_url = self.get_content_list()
self.save_content_list(content_list)
if __name__ == '__main__':
lagou = Lagou("python")
lagou.run()
# 翻頁
因為大多數(shù)時候募胃,我們爬數(shù)據(jù)都是有目的的,在這里畦浓,我將數(shù)據(jù)轉(zhuǎn)化成了csv格式保存的痹束,只需要按需修改即可啦。
源碼中讶请,是將爬去下來的數(shù)據(jù)保存在文件中祷嘶,這個文件被保存在當(dāng)前的項目路徑下,亦可按需修改
另外夺溢,我在最后又將源碼進(jìn)行修改论巍,通過分析網(wǎng)頁的url,發(fā)現(xiàn)搜索關(guān)鍵詞可以在url中修改风响,因此這里使用了format將其扣出嘉汰,只需要在實例化對象的時候,傳入想搜索的關(guān)鍵詞即可爬取到相關(guān)的職位
寫在最后:
通過這次爬蟲状勤,雖然很慢鞋怀。但是不得不承認(rèn),selenium更像是在模擬人真實的操作瀏覽器查看網(wǎng)頁持搜,因此此種方法不易被反爬蟲發(fā)現(xiàn)密似,但是是真的慢(用requests爬去json數(shù)據(jù),450條同樣的數(shù)據(jù)大概只需要30秒葫盼,而我用這種方案足足花了5分鐘)
另外残腌,selenium還有很多其他玩法,感興趣的朋友贫导,不妨開發(fā)腦洞废累,玩一下
好吧,暫時就說這么多脱盲,有空再聊邑滨,peace~
微信公眾號:TechBoard
個人博客:www.limiao.tech