python爬蟲(chóng)之利用Selenium+Requests爬取拉勾網(wǎng)
利用selenium+requests訪問(wèn)頁(yè)面爬取拉勾網(wǎng)招聘信息
二、分析url觀察頁(yè)面可知,頁(yè)面數(shù)據(jù)屬于動(dòng)態(tài)加載 所以現(xiàn)在我們通過(guò)抓包工具,獲取數(shù)據(jù)包
觀察其url和參數(shù)
url='https://www.lagou.com/jobs/positionAjax.json?px=default&needAddtionalResult=false'參數(shù):city=%E5%8C%97%E4%BA%AC ==》城市first=true ==》無(wú)用pn=1 ==》頁(yè)數(shù)kd=%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90 ==》商品關(guān)鍵詞
所以我們要想實(shí)現(xiàn)全站爬取,需要有city和頁(yè)數(shù)
三、獲取所有城市和頁(yè)數(shù)我們打開(kāi)拉勾網(wǎng),觀察后發(fā)現(xiàn),他的數(shù)據(jù)并不是完全展示的,比如說(shuō) 在城市篩選選擇全國(guó) 僅僅只顯示30頁(yè) 但總頁(yè)數(shù)是遠(yuǎn)遠(yuǎn)大于30頁(yè)的;我又選擇北京發(fā)現(xiàn)是30頁(yè)又選擇北京下的海淀區(qū)又是30頁(yè),可能我們無(wú)法把數(shù)據(jù)全部的爬取,但我們可以盡可能的將數(shù)據(jù)多的爬取
我們?yōu)榱双@取全站數(shù)據(jù),必然離不開(kāi)的有兩個(gè)參數(shù) 一個(gè)是城市一個(gè)是頁(yè)數(shù),所以我們利用selenium自動(dòng)化去獲取所有城市和對(duì)應(yīng)頁(yè)數(shù)
def City_Page(self): City_Page={} url='https://www.lagou.com/jobs/allCity.html?keyword=%s&px=default&companyNum=0&isCompanySelected=false&labelWords='%(self.keyword) self.bro.get(url=url) sleep(30) print('開(kāi)始獲取城市及其最大頁(yè)數(shù)') if '驗(yàn)證系統(tǒng)' in self.bro.page_source:sleep(40) html = etree.HTML(self.bro.page_source) city_urls = html.xpath(’//table[@class='word_list']//li/input/@value’) for city_url in city_urls:try: self.bro.get(city_url) if '驗(yàn)證系統(tǒng)' in self.bro.page_source:sleep(40) city=self.bro.find_element_by_xpath(’//a[@class='current_city current']’).text page=self.bro.find_element_by_xpath(’//span[@class='span totalNum']’).text City_Page[city]=page sleep(0.5)except: pass self.bro.quit() data = json.dumps(City_Page) with open('city_page.json', ’w’, encoding='utf-8')as f:f.write(data) return City_Page四、生成params參數(shù)
我們有了每個(gè)城市對(duì)應(yīng)的最大頁(yè)數(shù),就可以生成訪問(wèn)頁(yè)面所需的參數(shù)
def Params_List(self): with open('city_page.json', 'r')as f:data = json.loads(f.read()) Params_List = [] for a, b in zip(data.keys(), data.values()):for i in range(1, int(b) + 1): params = {’city’: a,’pn’: i,’kd’: self.keyword } Params_List.append(params) return Params_List五、獲取數(shù)據(jù)
最后我們可以通過(guò)添加請(qǐng)求頭和使用params url來(lái)訪問(wèn)頁(yè)面獲取數(shù)據(jù)
def Parse_Data(self,params): url = 'https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false' header={’referer’: ’https://www.lagou.com/jobs/list_%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90?labelWords=&fromSearch=true&suginput=’,’user-agent’: ’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36’,’cookie’:’’ } try:text = requests.get(url=url, headers=header, params=params).textif '頻繁' in text: print('操作頻繁,已被發(fā)現(xiàn) 當(dāng)前為第%d個(gè)params'%(i))data=json.loads(text)result=data['content']['positionResult']['result']for res in result: with open('.//lagou1.csv', 'a',encoding='utf-8') as f:writer = csv.DictWriter(f, res.keys())writer.writerow(res)sleep(1) except Exception as e:print(e)pass六、總結(jié)
盡管數(shù)據(jù)只顯示前30頁(yè),但數(shù)據(jù)還是未完全獲取
在利用selenium獲取城市最大頁(yè)數(shù)時(shí) 應(yīng)手動(dòng)登錄拉勾網(wǎng),并且其在訪問(wèn)過(guò)程中可能會(huì)出現(xiàn)驗(yàn)證系統(tǒng)需要驗(yàn)證
利用requests訪問(wèn)頁(yè)面獲取數(shù)據(jù)時(shí) 盡量sleep時(shí)間長(zhǎng)一點(diǎn),操作頻繁會(huì)封IP
到此這篇關(guān)于python爬蟲(chóng)之利用Selenium+Requests爬取拉勾網(wǎng)的文章就介紹到這了,更多相關(guān)Selenium+Requests爬取拉勾網(wǎng)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 利用CSS3新特性創(chuàng)建透明邊框三角2. html清除浮動(dòng)的6種方法示例3. CSS代碼檢查工具stylelint的使用方法詳解4. Vue3使用JSX的方法實(shí)例(筆記自用)5. vue實(shí)現(xiàn)將自己網(wǎng)站(h5鏈接)分享到微信中形成小卡片的超詳細(xì)教程6. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)7. 詳解CSS偽元素的妙用單標(biāo)簽之美8. 使用css實(shí)現(xiàn)全兼容tooltip提示框9. JavaScript數(shù)據(jù)類型對(duì)函數(shù)式編程的影響示例解析10. 不要在HTML中濫用div
