Python爬蟲,使用BeautifulSoup可以輕松解析頁面結果,下面是使用該方法爬取boss頁面的職位信息:包括職位名稱、薪資、地點、公司名稱、公司融資情況等信息。通過這個示例可以輕松看到BeautifulSoup的使用方法。
import requests
from bs4 import BeautifulSoup
from middlewares import get_random_proxy,get_random_agent
import time
class Boss_Spider(object):
def __init__(self, page=3):
self.proxies = []
self.verify_pro = []
self.page = page
self.headers = {}
#第一步:獲取首頁所有招聘連接
def Parse_pre(self):
base_url = 'https://www.zhipin.com/'
headers = get_random_agent()
proxy = get_random_proxy()
time.sleep(1)
resp = requests.get(base_url, headers=headers)
if resp.status_code == 200:
soup = BeautifulSoup(resp.text, 'lxml')
for job_menu in soup.find_all(class_='menu-sub'):
for li in job_menu.find_all('li'):
job_type = li.find('h4').get_text()
for job_list in li.find_all('a'):
job_sub = job_list.get_text()
job_uri = job_list['href']
for i in range(0,11):
job_url = base_url + job_uri + '?page=%d&ka=page-%d' %(i,i)
requests.get(job_url,headers=headers,proxies=proxy)
meta = {
'job_type': job_type,
'job_sub': job_sub,
}
self.Parse_index(meta=meta,url=job_url)
#爬取具體頁數據
def Parse_index(self,meta,url):
headers = get_random_agent()
proxy = get_random_proxy()
time.sleep(1)
resp = requests.get(url, headers=headers)
if resp.status_code == 200:
soup = BeautifulSoup(resp.text, 'lxml')
print(soup)
for li in soup.find(class_='job-list').find_all('li'):
print('###########')
position = li.find(class_='job-title').get_text()
salary = li.find(class_='red').get_text()
add = li.find('p').get_text()
need = li.find('p').find('em').get_text()
company_name = li.find(class_='company-text').find('a').get_text()
tag = li.find(class_='company-text').find('p')
print(position,"$$$",salary,"$$$",add,"$$$",need,"$$$",company_name,"$$$",tag)
if __name__ == '__main__':
b = Boss_Spider()
b.Parse_pre()
運行輸出結果如下:
后端開發 $$$ 15-30K $$$ 北京 朝陽區 朝外3-5年本科 $$$ $$$ 米花互動 $$$ 游戲不需要融資20-99人
###########
后端開發工程師 $$$ 35-55K $$$ 北京 朝陽區 望京經驗不限本科 $$$ $$$ 云賬戶 $$$ 移動互聯網C輪100-499人
###########
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
