前言💨
本文的文字及圖片來源於網絡,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯系我們以作處理。
前文內容💨
PS:如有需要 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 = 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 = 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文本的時候,會出現特殊字符無法保存需要把標題進行正則匹配,替換掉特殊字符;