python爬取这个网站https://www.nowcoder.com/jobs/intern/center?recruitType=2招聘信息100条,包括公司名称、岗位要求、工作类型、工作名称、薪资、城市名称、公司规模、岗位职责、每周工作天数等信息,并写入文本
时间: 2023-07-10 20:27:15 浏览: 152
好的,以下是代码实现。请确保您已经安装了 Python 和以下依赖库:requests、BeautifulSoup4。
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.nowcoder.com/jobs/intern/center?recruitType=2'
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'}
job_list = []
for i in range(1, 6):
params = {
'page': i
}
response = requests.get(url, headers=headers, params=params)
soup = BeautifulSoup(response.text, 'html.parser')
jobs = soup.select('.job-item')
for job in jobs:
job_dict = {}
job_dict['公司名称'] = job.select('.company-name')[0].text.strip()
job_dict['岗位要求'] = job.select('.job-requirement')[0].text.strip()
job_dict['工作类型'] = job.select('.job-type')[0].text.strip()
job_dict['工作名称'] = job.select('.job-name')[0].text.strip()
job_dict['薪资'] = job.select('.job-salary')[0].text.strip()
job_dict['城市名称'] = job.select('.job-city')[0].text.strip()
job_dict['公司规模'] = job.select('.job-scale')[0].text.strip()
job_dict['岗位职责'] = job.select('.job-description')[0].text.strip()
job_dict['每周工作天数'] = job.select('.job-days')[0].text.strip()
job_list.append(job_dict)
with open('job_info.txt', 'w', encoding='utf-8') as f:
for job in job_list:
for k, v in job.items():
f.write(k + ':' + v + '\n')
f.write('\n')
```
运行代码后,将会在当前目录下生成一个名为 `job_info.txt` 的文件,其中包含了爬取的招聘信息。
阅读全文