
step1:将网页源代码复制到txt文件
牛客的校招日历网址:https://www.nowcoder.com/school/schedule
我用的是edge浏览器,按F12查看源代码。
点击右上角画框的图标,鼠标移动到页面上找到想要的区域,可看到我想要的蓝色mask区域对应右面的<ul>
鼠标右键点击这部分源代码,复制元素到b.txt中
(复制前注意把页面多向下滚滚多加载些公司,不然只有第一页。



step2:运行代码
from bs4 import BeautifulSoup
import xlwt
if __name__ == '__main__':
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('1')
txt_path="b.txt"
head = ["公司", "内推", "网申", "笔试", "面试", "offer"]
for i, sth in enumerate(head):
worksheet.write(0, i, sth) #写入表头
f = open(txt_path, 'r', encoding='UTF-8')
txt = f.read() #读取html
soup = BeautifulSoup(txt, "html.parser")
comps = soup.find_all('div', class_="act-company-body")# 每个公司
for cindex, comp in enumerate(comps):
name = comp.find_all('h2') #提取公司名
worksheet.write(cindex + 1, 0, name[0].text)
times = comp.find_all('span', class_='act-company-time') #找出各个时间
for i, time in enumerate(times):
worksheet.write(cindex + 1, i + 1, time.text)
workbook.save("schedule.xls")
OVER
本文介绍了如何使用BeautifulSoup库从牛客网的校招日历页面抓取公司名称和时间信息,通过HTML源代码操作实现数据提取并保存为Excel。
790

被折叠的 条评论
为什么被折叠?



