拉勾网爬取失败?试试这一招

如果你爬过拉勾网就知道拉勾网有点难爬。

不愧是一家专为互联网从业者提供工作机会的招聘网站……

所以拉勾网使用的是什么反爬机制呢?一个是cookie限制,另一个是IP访问频率限制。

我在这次的爬取中遇到的反爬不是cookie限制,而是IP访问频率被限制了。

 

解决反爬虫

我选择了拉勾网自带岗位栏中的“数据运营”岗。

在第一次的尝试爬取中我遇到了这样的问题……

查看一下返回的响应页面发现……

即使加了请求头,每次只要爬到第6条数据时就会跳出验证页面,这是因为拉勾网检测到了同一IP的访问频率过快,于是触发了验证机制,需要输入验证信息才能获取我们想要的页面信息。

对于IP访问频率限制,使用IP代理是最理想的应对方法,不过也可以使用time模块来降低访问频率,缺点是速度很慢,如果需要爬取的数据不是很多的话可以采取这种方法。

以下我采用了延长访问频率的方式来尝试获取“数据运营”岗的全部招聘信息,结果没有报错!

具体的方法是:使用random模块中randint()函数随机获取秒数,再用time模块中sleep()函数将程序暂停一下,将其设置在请求网站后即可。

import random,time 
time.sleep(random.randint(10,15))

随机暂停的秒数设置在10~15s最好,因为我尝试过设置在5~10s结果还是被检测出来。

 

爬取拉勾网

爬取每个岗位以下招聘数据:

完整代码如下:

import requests,random,time,re
from bs4 import BeautifulSoup
import pandas as pd

# 定义空列表,用于存储信息
job_all={}
company_content=[]
industry_content=[]
job_content=[]
experience_content=[]
education_content=[]
salary_content=[]
detail_content=[]
url_content=[]

# 爬取1到8页的招聘信息
for i in range(1,9):
    url='https://www.lagou.com/guangzhou-zhaopin/shujuyunying/{}/'
    url=url.format(i)
    headers={'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
    res=requests.get(url,headers=headers,timeout=3)
    res.encoding=res.apparent_encoding
    soup=BeautifulSoup(res.text,'html.parser')
    urls=soup.find_all('div',class_='p_top') # 提取当页招聘岗位的详情链接
    # 遍历每一个详情链接,提取招聘岗位的公司、行业、岗位、经验要求、学历要求、工资、职责和工作要求
    for url in urls:
        url=url.find('a')['href']
        rep=requests.get(url,headers=headers,timeout=3)
        time.sleep(random.randint(10,15)) # 延迟程序运行,应对反爬虫
        rep.encoding=rep.apparent_encoding
        soup=BeautifulSoup(rep.text,'html.parser')
        company=soup.find('em',class_='fl-cn').text.strip() # 公司
        industry=soup.find('h4',class_='c_feature_name').text.strip() # 行业
        job=soup.find('h1',class_='name').text.strip() # 岗位
        detail=soup.find('div',class_='job-detail').text.strip() # 职责和工作要求
        request=soup.find('dd',class_='job_request') # 经验要求、学历要求和工资
        # 使用正则表达式进行提取
        request_match=re.match('^<dd .*?<span class=.*?>(.*?) </span>.*?span>/(.*?) /</span.*?span>(.*?) /</span.*?span>(.*?) /</span.*?span>(.*?)</span.*?h3>',str(request),re.S)
        experience=request_match.group(3) # 经验要求
        education=request_match.group(4) # 学历要求
        salary=request_match.group(1) # 工资
        # 添加岗位信息到列表
        company_content.append(company)
        industry_content.append(industry)
        job_content.append(job)
        experience_content.append(experience)
        education_content.append(education)
        salary_content.append(salary)
        detail_content.append(detail)
        url_content.append(url)

job_all['公司']=company_content
job_all['行业']=industry_content
job_all['岗位']=job_content
job_all['经验']=experience_content
job_all['学历']=education_content
job_all['工资']=salary_content
job_all['职责和要求']=detail_content
job_all['详情链接']=url_content

df=pd.DataFrame(job_all,columns=['公司','行业','岗位','经验','学历','工资','职责和要求','详情链接'])
df.to_excel('拉勾网数据运营岗.xlsx')

公众号:「Python编程小记」,持续推送学习分享,欢迎关注!

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
爬取拉勾的代码可以使用Python的requests和BeautifulSoup库来实现。首先,我们需要导入这两个库: ```python import requests from bs4 import BeautifulSoup ``` 接下来,我们可以使用requests库发送HTTP请求来获取拉勾的页面内容: ```python def get_page_content(url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} response = requests.get(url, headers=headers) if response.status_code == 200: return response.text else: return None ``` 然后,我们可以使用BeautifulSoup库来解析页面内容,提取我们需要的信息: ```python def parse_page(content): soup = BeautifulSoup(content, 'lxml') job_list = soup.find_all('li', attrs={'class': 'con_list_item'}) for job in job_list: job_title = job.find('h3').find('a').text job_salary = job.find('span', attrs={'class': 'money'}).text job_company = job.find('div', attrs={'class': 'company'}).find('a').text print('岗位:', job_title) print('薪资:', job_salary) print('公司:', job_company) print('-------------------------------------') ``` 最后,我们可以将以上两个函数组合起来进行爬取: ```python if __name__ == '__main__': url = 'https://www.lagou.com/zhaopin/Python/?labelWords=label' content = get_page_content(url) if content: parse_page(content) ``` 这样,我们就可以使用上述代码对拉勾Python招聘信息进行爬取了。记得要遵守站的爬取规则,并且注意不要对站进行恶意操作,以免违反相关法律规定。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值