前言
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯系我們以作處理,
Python爬蟲、資料分析、網站開發等案例教程視頻免費在線觀看
https://space.bilibili.com/523606542
前文內容
Python爬蟲新手入門教學(一):爬取豆瓣電影排行資訊
Python爬蟲新手入門教學(二):爬取小說
Python爬蟲新手入門教學(三):爬取鏈家二手房資料
基本開發環境
- Python 3.6
- Pycharm
相關模塊的使用
- requests
- parsel
- csv
- re
安裝Python并添加到環境變數,pip安裝需要的相關模塊即可,
一、明確需求
爬取內容:
- 招聘標題
- 公司
- 薪資
- 城市區域
- 作業經驗要求、學歷要求、招聘人數、發布時間、公司福利
- 崗位職責、任職要求
二、請求網頁,先獲取所有招聘資訊的詳情url地址
使用開發者工具發現網頁加載出來的內容是亂代碼的,這也意味著等會再爬取的時候,是需要轉碼的,這樣看是看不出自己想要的內容網頁是否有回傳資料,可以復制網頁中的資料,在網頁源代碼里面搜索,
沒有結果,那么我們就可以搜索詳情鏈接的ID
里面不僅有ID 還有詳情url地址,用正則運算式匹配出ID,然后再拼接url,如果匹配出url地址的話,需要再轉一次,
特別宣告:
因為網站原因,每一個招聘詳細頁面url地址,僅僅只是ID的變化,如果ID不是唯一變化值的時候,那取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)
簡單總結
列印 response.text 可以在pycharm里面使用正則匹配規則,可以測驗是否有匹配到資料,詳情如下圖所示
三、決議招聘資訊資料,提取內容
每頁第一個招聘資訊是沒有薪資的,沒有薪資待遇的,對于沒有薪資的招聘資訊,我們就自動跳過就好了,所以需要先判斷一下,
其次前面有說過,網頁查看內容是有亂碼的,需要進行轉碼,
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)
# 進行轉碼
response.encoding = response.apparent_encoding
html_data = https://www.cnblogs.com/hhh188764/p/response.text
selector = parsel.Selector(html_data)
# 薪資
money = selector.css('.cn strong::text').get()
# 判斷如果有薪資繼續提取相關內容
if money:
# 標題
title = selector.css('.cn h1::attr(title)').get()
# 公司
cname = selector.css('.cname a:nth-child(1)::attr(title)').get()
# 上海-徐匯區 | 5-7年經驗 | 本科 | 招1人 | 01-25發布
info_list = selector.css('p.msg.ltype::attr(title)').get().split(' | ')
city = info_list[0] # 城市
exp = info_list[1] # 經驗要求
edu = info_list[2] # 學歷要求
people = info_list[3] # 招聘人數
date = info_list[4] # 發布時間
# 福利
boon_list = selector.css('.t1 span::text').getall()
boon_str = '|'.join(boon_list)
# 崗位職責: 任職要求:
position_list = selector.css('.job_msg p::text').getall()
position = '\n'.join(position_list)
dit = {
'標題': title,
'公司': cname,
'城市': city,
'經驗要求': exp,
'學歷要求': edu,
'薪資': money,
'福利': boon_str,
'招聘人數': people,
'發布時間': date,
'詳情地址': page_url,
}
四、保存資料(資料持久化)
關于薪資待遇、公司地址這些就用csv保存,崗位職責和任職要求就保存文本格式吧,這樣看起來會稍微舒服一些,
保存csv
f = open('python招聘.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=['標題', '公司', '城市', '經驗要求', '學歷要求',
'薪資', '福利', '招聘人數', '發布時間',
'詳情地址'])
csv_writer.writeheader()
保存txt
txt_filename = '崗位職責\\' + f'{cname}招聘{title}資訊.txt'
with open(txt_filename, mode='a', encoding='utf-8') as f:
f.write(position)
五、多頁資料爬取
'''
if __name__ == '__main__':
'''
第一頁地址:
https://search.51job.com/list/010000%252c020000%252c030200%252c040000%252c090200,000000,0000,00,9,99,python,2,1.html
第二頁地址:
https://search.51job.com/list/010000%252c020000%252c030200%252c040000%252c090200,000000,0000,00,9,99,python,2,2.html
第三頁地址:
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
主函式代碼
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 = https://www.cnblogs.com/hhh188764/p/response.text
selector = parsel.Selector(html_data)
# 薪資
money = selector.css('.cn strong::text').get()
# 判斷如果有薪資繼續提取相關內容
if money:
# 標題
title = selector.css('.cn h1::attr(title)').get()
# 公司
cname = selector.css('.cname a:nth-child(1)::attr(title)').get()
# 上海-徐匯區 | 5-7年經驗 | 本科 | 招1人 | 01-25發布
info_list = selector.css('p.msg.ltype::attr(title)').get().split(' | ')
if len(info_list) == 5:
city = info_list[0] # 城市
exp = info_list[1] # 經驗要求
edu = info_list[2] # 學歷要求
people = info_list[3] # 招聘人數
date = info_list[4] # 發布時間
# 福利
boon_list = selector.css('.t1 span::text').getall()
boon_str = '|'.join(boon_list)
# 崗位職責: 任職要求:
position_list = selector.css('.job_msg p::text').getall()
position = '\n'.join(position_list)
dit = {
'標題': title,
'公司': cname,
'城市': city,
'經驗要求': exp,
'學歷要求': edu,
'薪資': money,
'福利': boon_str,
'招聘人數': people,
'發布時間': date,
'詳情地址': page_url,
}
new_title = change_title(title)
txt_filename = '崗位職責\\' + 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)
總結
① 有一些公司招聘并沒有寫學歷要求,直接爬取會進行保存,超出索引范圍,要進行判斷;
② 保存txt文本的時候,會出現特殊字符無法保存需要把標題進行正則匹配,替換掉特殊字符;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/252903.html
標籤:Python

