import requests from bs4 import BeautifulSoup import re import pandas as pd # 目标招聘网站的URL(以智联招聘为例) url = "https://www.58.com/ppezp2023083158986/" # 请求头,模拟浏览器访问 headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36" } # 发送HTTP请求,获取网页内容 response = requests.get(url, headers=headers) html_content = response.text # 使用BeautifulSoup解析HTML soup = BeautifulSoup(html_content, "html.parser") # 查找所有招聘信息的标签(根据实际网页结构调整) job_list = soup.find_all("div", class_="joblist__item") print(job_list) # 初始化一个空列表,用于存储提取的数据 jobs_data = [] # 正则表达式模式,用于提取薪资范围(示例) salary_pattern = re.compile(r"(\d+-\d+)千/月") # 遍历每个招聘信息 for job in job_list: # 提取职位名称 job_title = job.find("span", class_="jobname__title").text.strip() print(job_title) # 提取公司名称 company_name = job.find("a", class_="company__title").text.strip() # 提取工作地点 location = job.find("span", class_="job__location").text.strip() # 提取薪资范围(使用正则表达式) salary_text = job.find("span", class_="job__salary").text.strip() salary_match = salary_pattern.search(salary_text) salary = salary_match.group(1) if salary_match else "面议" # 提取工作经验要求 experience = job.find("span", class_="job__experience").text.strip() # 提取学历要求 education = job.find("span", class_="job__education").text.strip() # 提取职位描述 description = job.find("div", class_="job__desc").text.strip() # 将提取的数据存储为字典 job_info = { "职位名称": job_title, "公司名称": company_name, "工作地点": location, "薪资范围": salary, "工作经验": experience, "学历要求": education, "职位描述": description } # 将字典添加到列表中 jobs_data.append(job_info) print(job_info) # 将数据存储到DataFrame中 df = pd.DataFrame(jobs_data) # 保存到Excel文件 df.to_excel("招聘信息.xlsx", index=False) print("数据爬取完成,已保存到招聘信息.xlsx")
时间: 2025-03-07 19:13:50 浏览: 109
### Python 网络爬虫抓取招聘信息并保存至 Excel
为了实现从招聘网站抓取招聘信息并将信息保存到 Excel 文件的功能,可以采用 `requests` 库代替 `urllib.request` 来发送 HTTP 请求,因为前者更易于使用;利用 `BeautifulSoup` 解析 HTML 文档以提取所需的信息;最后通过 `openpyxl` 或者 `pandas` 将数据写入 Excel 表格。
下面是一段完整的示例代码:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
def fetch_job_listings(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
jobs = []
for job_card in soup.find_all('div', class_='job-card'):
title_element = job_card.select_one('.title a')
company_element = job_card.select_one('.company-name span')
location_element = job_card.select_one('.location')
if all([title_element, company_element, location_element]):
job_info = {
'Title': title_element.get_text(strip=True),
'Company': company_element.get_text(strip=True),
'Location': location_element.get_text(strip=True).strip()
}
jobs.append(job_info)
return jobs
url = "https://example.com/jobs" # 替换为目标招聘网站的实际URL
jobs_data = fetch_job_listings(url)
df = pd.DataFrame(jobs_data)
output_file_path = './recruitment_information.xlsx'
df.to_excel(output_file_path, index=False)
print(f'Jobs data has been saved to {output_file_path}')
```
这段脚本定义了一个函数 `fetch_job_listings()` ,它接收一个 URL 参数作为输入,并返回包含职位名称、公司名以及工作地点在内的字典列表。接着创建 Pandas DataFrame 对象以便于后续处理和导出操作。最终调用 `.to_excel()` 方法把 DataFrame 的内容写出到本地磁盘上的 Excel 文件中[^1]。
请注意,在实际应用此代码前需调整 CSS Selectors (`class_`, `select_one`) 以匹配目标页面的具体结构。此外还需确保遵守目标站点的服务条款,合理设置请求频率以免给服务器造成过大负担[^2]。
阅读全文
相关推荐















