本文共 4392 字,大约阅读时间需要 14 分钟。
来到BOOS直聘
偷偷告诉大家一个小技巧:虽然被禁止访问了,但登录后就又可以访问了,嘿嘿!可惜我当时不知道,事后才发现,可惜。
现在这样只能使用IP代理了
使用IP代理参考以下文章建立boos数据库
CREATE TABLE `boos` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(100) DEFAULT NULL, `company` varchar(100) DEFAULT NULL, `price` varchar(100) DEFAULT NULL, `education` varchar(100) DEFAULT NULL, `text` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci, `introduce` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci, `address` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=99 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
import requestsfrom bs4 import BeautifulSoupimport timeimport pymysql#控制爬取页数num = 2#插入语句sql = "insert into boos(id,title, company, price, education, text, introduce ,address) values(null,%s,%s,%s,%s,%s,%s,%s)"#请求头headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36", #爬取boos直聘cookie必不可少,参考图1 "cookie": ""}#IP代理proxy = { 'https': '61.178.118.86:8080'}#爬取数据def Crawling(cur,conn,response): #引入全局变量 global num global sql # 使用 lxml XML解析器 data_list = BeautifulSoup(response.text, "lxml") #拿到所有的li标签然后遍历,参考图2 #由于li没有class什么的,我们找到搜索li的父标签定位,再找下面的所有li li_list = data_list.find(class_="job-list").find_all("li") #遍历 for data in li_list: bs = BeautifulSoup(str(data), "lxml") #职位,参考图3 title = bs.find("a")["title"].strip() url = "https://www.zhipin.com/" + bs.find("a")['href'] # 公司,图4 company = bs.find(class_="company-text").find(class_="name").text # 公司福利 图5 education = bs.find(class_="info-desc").text # 薪资 图6 price = bs.find(class_="red").text # print(title+"--"+company+"--"+price+"--"+education) # # 请求详情页,进行数据爬取 time.sleep(1) page_source = requests.get(url=url, headers=headers) page_source.encoding = "utf-8" page_bs = BeautifulSoup(str(page_source.text), "lxml") # 岗位职责,图7 text = page_bs.find(class_="text").text.strip() #print(text) #print("+"*100) # 公司介绍,参考图8 #有的公司没有介绍,爬取的时候会异常,我们呢处理异常,没有的时候直接给无介绍 try: #因为这里的class值也是text,由于find的特性只会返回匹配到的第一个值,所以我们选择定位他的父标签,再找它 introduce = page_bs.find(class_="job-sec company-info").find(class_="text").text.strip() except: introduce = "无介绍" # 工作地址,图9 #有的公司地址后带有502,我们把它替换成空串 address = page_bs.find(class_="location-address").text.replace("502","") #执行sql,提交事务 cur.execute(sql, (title, company, price, education, text, introduce, address)) conn.commit() #多页爬取 if num < 4: #链接分析,图10 next_url = "https://www.zhipin.com/c100010000/?query=python&page="+str(num)+"&ka=page-"+str(num) num += 1 next_data = requests.get(url=next_url,headers=headers,proxies=proxy) next_data.encoding = "utf-8" #爬取 Crawling(cur,conn,next_data) else: return cur,conn#初始化mysql连接def init_mysql(): dbparams = { 'host': '127.0.0.1', 'port': 3306, 'user': '数据库账号', 'password': '数据库密码', 'database': 'boos', #数据库名 'charset': 'utf8' } conn = pymysql.connect(**dbparams) cur = conn.cursor() return cur,conn#关闭数据库连接def close(cur,conn): cur.close() conn.close()#起始if __name__ == "__main__": #print("="*40) #防止请求频繁,关闭多余链接,可参考博主的文章 requests.DEFAULT_RETRIES = 5 s = requests.session() s.keep_alive = False #请求链接,只需更改url即可爬取自己想爬取的数据 start_url = "https://www.zhipin.com/c100010000/?query=python&page=1&ka=page-1" response = requests.get(url=start_url, headers=headers,proxies=proxy) time.sleep(2) response.encoding = "utf-8" # print("="*40) #查看请求状态码,200为成功 print(response.status_code) cur,conn = init_mysql() #爬取数据 cur,conn = Crawling(cur,conn,response) #关闭数据库连接 close(cur,conn)
图1
图2
图4
图6
图7
图8
'NoneType' object has no attribute 'find_all'
BOOS直聘的反爬虫机制确实厉害,如果报以上错误,可以通过更换cookie或者更换代理IP来解决
实在不行,那就用不用代理ip了,还是用本机的,小技巧在上面已经教给大家了。
博主会持续更新,有兴趣的小伙伴可以点赞、关注和收藏下哦,你们的支持就是我创作最大的动力!
本文爬虫源码已由 GitHub 已经收录(内涵更多本博文没有的爬虫,有兴趣的小伙伴可以看看),之后会持续更新,欢迎Star。
转载地址:http://vdolz.baihongyu.com/