您正在学习的是试看内容,报名后可学习全部内容
报名课程
当前课程未解锁
高能实战:2020年你高薪了么
代码参考:
'''
抓取拉钩工作列表
杭州默认
pip3 install requests
pip3 install beautifulsoup4
'''
import requests # 引入requests库
from bs4 import BeautifulSoup # 引入BeautifulSoup库
from time import sleep # 引入time库中的sleep
def fetch_page(category: str, page: int, city = 'hangzhou'): # 定义 fetch_page 函数,参数分别是category(分类),page(页码),city(城市,双拼)
r = requests.get('https://www.lagou.com/%s-zhaopin/%s/%d/?filterOption=3'%(city, category, page)) # 将获取的参数拼装成url,并从远端服务器获取内容
html = r.text # 获取返回数据中的html内容
soup = BeautifulSoup(html, 'html.parser') # 将html放入beautifulsoup库处理
for item in soup.select('ul.item_con_list li.con_list_item'): # 用css选择器定位工作信息元素
print("名称: " + item.select_one('h3').get_text()) # 获取工作名称并打印
print("网址: " + item.select_one('a.position_link')['href']) # 获取工作网址并打印
print("公司: " + item.select_one('.company_name a').get_text()) # 获取公司名称并打印
print("薪水: " + item.select_one('.money').get_text()) # 获取薪水范围并打印
print("==============") # 打印分隔符
for page in range(1, 4): # 选取1-4页获取
fetch_page('Python', page) # 开始获取信息
sleep(2) # 每获取一页停留2秒钟,防止被服务器发现为恶意爬虫