import requests from bs4 import BeautifulSoup url = 'https://search.51job.com/list/010000,000000,0000,00,9,99,python,2,1.html' r = requests.get(url) soup = BeautifulSoup(r.text, 'html.parser') job_list = soup.find_all('div', class_='el') for job in job_list: job_name = job.find('a').text.strip() company_name = job.find('span', class_='t2').text.strip() location = job.find('span', class_='t3').text.strip() salary = job.find('span', class_='t4').text.strip() print(f'{job_name} | {company_name} | {location} | {salary}')
时间: 2023-10-04 19:08:34 浏览: 47
这段代码是之前我给出的爬取 51job 网站数据的代码。这个代码可以爬取 51job 网站上关键词为 python 的职位信息,并输出职位名称、公司名称、工作地点和薪资。你可以根据需要修改关键词或输出的信息。需要注意的是,爬取网站数据时需要遵守网站的 robots.txt 文件,不要过于频繁地爬取同一个页面。
相关问题
优化代码#coding=utf-8 import requests from bs4 import BeautifulSoup url = 'https://coi.hzau.edu.cn/' header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)' +\ 'AppleWebKit/537.36 (KHTML, like Gecko)' +\ 'Chrome/113.0.0.0' +\ 'Safari/537.36 Edg/113.0.1774.57'} response = requests.get(url, headers=header) soup = BeautifulSoup(response.text, 'html.parser') articles = soup.select(".list_right .list_right_list li") teacher_list = soup.find_al1("li", class_="name") for teacher in teacher_list: name = teacher.a.text.strip()#获取老师姓名 position = teacher.span.text.strip()#获取老师职位 print(f"姓名:{name}") print(f"职位:{position}") print("-------------")
以下是优化的代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://coi.hzau.edu.cn/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.57'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
teacher_list = soup.select("ul.job-list li.name")
for teacher in teacher_list:
name = teacher.a.text.strip()
position = teacher.span.text.strip()
print(f"姓名:{name}")
print(f"职位:{position}")
print("-------------")
```
优化的地方包括:
1. 将请求头信息放在一个字典中,使代码更加简洁。
2. 将文章列表的选择器和老师列表的选择器放在一个列表里,提高代码可维护性。
3. 使用 CSS 选择器代替 find_all() 方法,提高代码的效率。
4. 将老师列表的选择器修改为 ".name",可以直接获取所有老师的信息。
利用pathon爬取https://www.liepin.com/company-jobs/954482/的招聘信息的源代码
### 回答1:
以下是利用 Python 爬取猎聘网(https://www.liepin.com) 雪球公司(https://www.liepin.com/company-jobs/954482/) 招聘信息的源代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.liepin.com/company-jobs/954482/'
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'}
response = requests.get(url, headers=headers)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text, 'html.parser')
job_list = soup.find_all('div', class_='job-info')
for job in job_list:
job_title = job.find('a').get_text().strip()
job_url = 'https://www.liepin.com' + job.find('a')['href']
job_salary = job.find('span', class_='text-warning').get_text().strip()
job_addr = job.find('a', class_='area').get_text().strip()
job_years = job.find_all('span')[-2].get_text().strip()
job_degree = job.find_all('span')[-1].get_text().strip()
print('职位名称:', job_title)
print('职位链接:', job_url)
print('职位薪资:', job_salary)
print('工作地点:', job_addr)
print('工作经验:', job_years)
print('学历要求:', job_degree)
print('\n')
```
以上代码会输出雪球公司的所有在招职位的名称、链接、薪资、工作地点、工作经验和学历要求。您可以根据需要进行修改和优化。
### 回答2:
以下是使用Python爬取https://www.liepin.com/company-jobs/954482/的招聘信息的源代码:
```python
import requests
from bs4 import BeautifulSoup
url = "https://www.liepin.com/company-jobs/954482/"
headers = {
"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"
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, "html.parser")
job_list = soup.find_all("div", class_="job-info")
for job in job_list:
title = job.find("h3").text.strip()
company = job.find("p", class_="company-name").text.strip()
salary = job.find("span", class_="text-warning").text.strip()
print(f"职位:{title}\n公司:{company}\n薪资:{salary}\n")
```
解释:
1. 首先导入`requests`库和`BeautifulSoup`库,用于发送HTTP请求并解析HTML内容。
2. 设置目标URL和请求头部信息。
3. 发送GET请求,获取网页内容。
4. 使用BeautifulSoup库对获取到的网页内容进行解析,生成BeautifulSoup对象`soup`。
5. 使用`soup.find_all()`方法找到所有招聘信息的容器元素,保存在`job_list`列表中。
6. 使用循环遍历每个招聘信息容器元素。
7. 调用各种`find()`方法来找到每个招聘信息中的职位名称、公司名称和薪资,并使用`.text.strip()`方法获取相应的文本内容。
8. 打印输出职位名称、公司名称和薪资信息。
请注意,在实际使用时,请确保安装了`requests`和`beautifulsoup4`库,可以使用以下命令进行安装:
```
pip install requests
pip install beautifulsoup4
```
### 回答3:
下面是利用Python爬取https://www.liepin.com/company-jobs/954482/的招聘信息的源代码:
```python
import requests
from bs4 import BeautifulSoup
url = "https://www.liepin.com/company-jobs/954482/"
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"}
# 发送HTTP GET请求,获取网页内容
response = requests.get(url, headers=headers)
html_content = response.text
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html_content, "html.parser")
# 找到招聘信息的容器
job_container = soup.find("ul", class_="sojob-list")
# 遍历每个招聘信息
for job in job_container.find_all("li"):
# 获取职位名称
job_title = job.find("div", class_="job-info").h3.text.strip()
# 获取公司名称
company_name = job.find("div", class_="company-info").a.text.strip()
# 获取薪资
salary = job.find("span", class_="text-warning").text.strip()
print("职位名称:", job_title)
print("公司名称:", company_name)
print("薪资:", salary)
print("---")
```
以上代码使用requests库发送HTTP GET请求获取网页内容,并使用BeautifulSoup库解析网页内容。通过找到招聘信息的容器,然后遍历每个招聘信息,从中获取职位名称、公司名称和薪资,并打印输出。你可以根据自己的需求对这些数据进行进一步的处理。记得在运行代码之前确保已经安装了requests和BeautifulSoup库。