1. 程式人生 > 實用技巧 >Python爬取招聘網資訊

Python爬取招聘網資訊

1、資料來源:職友集

2、程式碼

import requests
import openpyxl
import time
from bs4 import BeautifulSoup #用於解析和提取網頁資料的
lst=[]#列表
def send_request(id,page):
    url = 'https://www.jobui.com/company/{0}/jobs/p{1}/'.format(id,page)
    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0
'} # 建立頭部資訊 resp = requests.get(url, headers=headers) return resp.text #進行資料的提取 def parse_html(html): bs=BeautifulSoup(html,'html.parser') #得到Bea--的物件了 job_lst=bs.find_all('div',class_='c-job-list')#因為class是關鍵字,所以加一個下劃線 #print(job_lst) for item in job_lst: #分別遍歷每一個職位資料 name=item.find('
h3').text#職位的名稱 div_tag=item.find('div',class_='job-desc') #print(div_tag) span_tag=div_tag.find_all('span') #print(span_tag[0].text) url=item.find('a',class_='job-name')['href']#提取class樣式為job-name的a標籤,獲取屬性href的值 lst.append([name,span_tag[0].text,span_tag[1].text,'
https://www.jobui.com'+url]) #儲存excel def save(lst): wk = openpyxl.Workbook() sheet = wk.active for item in lst: sheet.append(item) wk.save('招聘資訊.xlsx') #啟動爬蟲程式 def start(id,pages): for page in range(1,pages+1): resp_data=send_request(id,page) parse_html(resp_data) time.sleep(2) save(lst) if __name__=='__main__': id='10375749' pages=1 start(id,pages)
zhiyou.py

3、結果