您好,登錄后才能下訂單哦!
這篇文章主要介紹“Python怎么爬取前程無(wú)憂招聘信息”,在日常操作中,相信很多人在Python怎么爬取前程無(wú)憂招聘信息問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”P(pán)ython怎么爬取前程無(wú)憂招聘信息”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
Python 3.6
Pycharm
requests
parsel
csv
re
安裝Python并添加到環(huán)境變量,pip安裝需要的相關(guān)模塊即可。
爬取前程無(wú)憂招聘信息
爬取內(nèi)容:
招聘標(biāo)題
公司
薪資
城市區(qū)域
工作經(jīng)驗(yàn)要求、學(xué)歷要求、招聘人數(shù)、發(fā)布時(shí)間、公司福利
崗位職責(zé)、任職要求
使用開(kāi)發(fā)者工具發(fā)現(xiàn)網(wǎng)頁(yè)加載出來(lái)的內(nèi)容是亂代碼的,這也意味著等會(huì)再爬取的時(shí)候,是需要轉(zhuǎn)碼的,這樣看是看不出自己想要的內(nèi)容網(wǎng)頁(yè)是否有返回?cái)?shù)據(jù),可以復(fù)制網(wǎng)頁(yè)中的數(shù)據(jù),在網(wǎng)頁(yè)源代碼里面搜索。
沒(méi)有結(jié)果,那么我們就可以搜索詳情鏈接的ID
里面不僅有ID 還有詳情url地址。用正則表達(dá)式匹配出ID,然后再拼接url,如果匹配出url地址的話,需要再轉(zhuǎn)一次。
特別聲明:
因?yàn)榫W(wǎng)站原因,每一個(gè)招聘詳細(xì)頁(yè)面url地址,僅僅只是ID的變化,如果ID不是唯一變化值的時(shí)候,那取url地址更好。
import requests import re def get_response(html_url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36', } response = requests.get(url=html_url, headers=headers) return response def get_id(html_url): response = get_response(html_url) result = re.findall('"jobid":"(\d+)"', response.text) print(response.text) print(result) if __name__ == '__main__': url = 'https://search.51job.com/list/010000%252C020000%252C030200%252C040000%252C090200,000000,0000,00,9,99,python,2,1.html' get_id(url)
簡(jiǎn)單總結(jié)
打印 response.text 可以在pycharm里面使用正則匹配規(guī)則,可以測(cè)試是否有匹配到數(shù)據(jù)。詳情如下圖所示
每頁(yè)第一個(gè)招聘信息是沒(méi)有薪資的,沒(méi)有薪資待遇的,對(duì)于沒(méi)有薪資的招聘信息,我們就自動(dòng)跳過(guò)就好了,所以需要先判斷一下。
其次前面有說(shuō)過(guò),網(wǎng)頁(yè)查看內(nèi)容是有亂碼的,需要進(jìn)行轉(zhuǎn)碼。
def get_content(html_url): result = get_id(html_url) for i in result: page_url = f'https://jobs.51job.com/shanghai-xhq/{i}.html?s=01&t=0' response = get_response(page_url) # 進(jìn)行轉(zhuǎn)碼 response.encoding = response.apparent_encoding html_data = response.text selector = parsel.Selector(html_data) # 薪資 money = selector.css('.cn strong::text').get() # 判斷如果有薪資繼續(xù)提取相關(guān)內(nèi)容 if money: # 標(biāo)題 title = selector.css('.cn h2::attr(title)').get() # 公司 cname = selector.css('.cname a:nth-child(1)::attr(title)').get() # 上海-徐匯區(qū) | 5-7年經(jīng)驗(yàn) | 本科 | 招1人 | 01-25發(fā)布 info_list = selector.css('p.msg.ltype::attr(title)').get().split(' | ') city = info_list[0] # 城市 exp = info_list[1] # 經(jīng)驗(yàn)要求 edu = info_list[2] # 學(xué)歷要求 people = info_list[3] # 招聘人數(shù) date = info_list[4] # 發(fā)布時(shí)間 # 福利 boon_list = selector.css('.t1 span::text').getall() boon_str = '|'.join(boon_list) # 崗位職責(zé): 任職要求: position_list = selector.css('.job_msg p::text').getall() position = '\n'.join(position_list) dit = { '標(biāo)題': title, '公司': cname, '城市': city, '經(jīng)驗(yàn)要求': exp, '學(xué)歷要求': edu, '薪資': money, '福利': boon_str, '招聘人數(shù)': people, '發(fā)布時(shí)間': date, '詳情地址': page_url, }
關(guān)于薪資待遇、公司地址這些就用csv保存,崗位職責(zé)和任職要求就保存文本格式吧,這樣看起來(lái)會(huì)稍微舒服一些。
保存csv
f = open('python招聘.csv', mode='a', encoding='utf-8', newline='') csv_writer = csv.DictWriter(f, fieldnames=['標(biāo)題', '公司', '城市', '經(jīng)驗(yàn)要求', '學(xué)歷要求', '薪資', '福利', '招聘人數(shù)', '發(fā)布時(shí)間', '詳情地址']) csv_writer.writeheader()
保存txt
txt_filename = '崗位職責(zé)\\' + f'{cname}招聘{title}信息.txt' with open(txt_filename, mode='a', encoding='utf-8') as f: f.write(position)
''' if __name__ == '__main__': ''' 第一頁(yè)地址: https://search.51job.com/list/010000%252c020000%252c030200%252c040000%252c090200,000000,0000,00,9,99,python,2,1.html 第二頁(yè)地址: https://search.51job.com/list/010000%252c020000%252c030200%252c040000%252c090200,000000,0000,00,9,99,python,2,2.html 第三頁(yè)地址: https://search.51job.com/list/010000%252c020000%252c030200%252c040000%252c090200,000000,0000,00,9,99,python,2,3.html ''' for page in range(1, 11): url = f'https://search.51job.com/list/010000%252C020000%252C030200%252C040000%252C090200,000000,0000,00,9,99,python,2,{page}.html' get_content(url)
正則匹配替換特殊字符
def change_title(title): pattern = re.compile(r"[\/\\\:\*\?\"\<\>\|]") # '/ \ : * ? " < > |' new_title = re.sub(pattern, "_", title) # 替換為下劃線 return new_title
主函數(shù)代碼
def main(html_url): result = get_id(html_url) for i in result: page_url = f'https://jobs.51job.com/shanghai-xhq/{i}.html?s=01&t=0' response = get_response(page_url) response.encoding = response.apparent_encoding html_data = response.text selector = parsel.Selector(html_data) # 薪資 money = selector.css('.cn strong::text').get() # 判斷如果有薪資繼續(xù)提取相關(guān)內(nèi)容 if money: # 標(biāo)題 title = selector.css('.cn h2::attr(title)').get() # 公司 cname = selector.css('.cname a:nth-child(1)::attr(title)').get() # 上海-徐匯區(qū) | 5-7年經(jīng)驗(yàn) | 本科 | 招1人 | 01-25發(fā)布 info_list = selector.css('p.msg.ltype::attr(title)').get().split(' | ') if len(info_list) == 5: city = info_list[0] # 城市 exp = info_list[1] # 經(jīng)驗(yàn)要求 edu = info_list[2] # 學(xué)歷要求 people = info_list[3] # 招聘人數(shù) date = info_list[4] # 發(fā)布時(shí)間 # 福利 boon_list = selector.css('.t1 span::text').getall() boon_str = '|'.join(boon_list) # 崗位職責(zé): 任職要求: position_list = selector.css('.job_msg p::text').getall() position = '\n'.join(position_list) dit = { '標(biāo)題': title, '公司': cname, '城市': city, '經(jīng)驗(yàn)要求': exp, '學(xué)歷要求': edu, '薪資': money, '福利': boon_str, '招聘人數(shù)': people, '發(fā)布時(shí)間': date, '詳情地址': page_url, } new_title = change_title(title) txt_filename = '崗位職責(zé)\\' + f'{cname}招聘{new_title}信息.txt' with open(txt_filename, mode='a', encoding='utf-8') as f: f.write(position) csv_writer.writerow(dit) print(dit)
到此,關(guān)于“Python怎么爬取前程無(wú)憂招聘信息”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。