以下是一个简单的示例代码,用于爬取 boss 直聘的岗位信息:\n\npython\nimport requests\nfrom bs4 import BeautifulSoup\n\ndef get_job_list():\n url = 'https://www.zhipin.com/job_detail/?query=python&page=1'\n headers = {\n 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'\n }\n\n response = requests.get(url, headers=headers)\n soup = BeautifulSoup(response.text, 'html.parser')\n job_list = soup.find_all('div', class_='job-primary')\n\n for job in job_list:\n job_title = job.find('div', class_='job-title').text.strip()\n job_salary = job.find('span', class_='red').text.strip()\n job_company = job.find('div', class_='company-text').a.text.strip()\n job_location = job.find('div', class_='info-primary').p.text.strip()\n job_experience = job.find('div', class_='info-primary').p.next_sibling.text.strip()\n\n print('职位:', job_title)\n print('薪资:', job_salary)\n print('公司:', job_company)\n print('地点:', job_location)\n print('经验:', job_experience)\n print('---')\n\nif __name__ == '__main__':\n get_job_list()\n\n\n请注意,爬取网页内容需要使用合适的 User-Agent,这里我们使用了一个常见的浏览器 User-Agent。同时,还需要安装 requests 和 beautifulsoup4 这两个库。你可以使用 pip 来安装它们:\n\n\npip install requests beautifulsoup4\n\n\n此代码仅仅是一个简单的示例,你可以根据自己的需求进行修改和优化。


原文地址: https://www.cveoy.top/t/topic/pOni 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录