python爬取前程无忧招聘网站数据并存入csv文件中源代码
时间: 2024-06-10 20:09:27 浏览: 134
我可以回答这个问题。以下是一个简单的Python代码示例,可以爬取前程无忧招聘网站的数据,并将其存储在CSV文件中:
``` python
import requests
import csv
from bs4 import BeautifulSoup
url = 'https://search.51job.com/list/010000,000000,0000,00,9,99,Python%2B%E7%88%AC%E8%99%AB,2,1.html'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
job_list = soup.find_all('div', {'class': 'el'})
rows = []
for job in job_list:
title = job.find('a').text.strip()
company = job.find('span', {'class': 't2'}).text.strip()
location = job.find('span', {'class': 't3'}).text.strip()
salary = job.find('span', {'class': 't4'}).text.strip()
rows.append([title, company, location, salary])
with open('jobs.csv', 'w', newline='', encoding='utf-8-sig') as f:
writer = csv.writer(f)
writer.writerows(rows)
print('Done')
```
该代码使用了Python的requests、BeautifulSoup和csv模块,首先发送HTTP请求获取网页内容,然后使用BeautifulSoup解析HTML并提取所需的职位信息,最后将数据写入CSV文件中。请注意,此代码仅供参考,并且需要根据实际情况进行修改。
阅读全文