【Python】简单的爬虫抓取

效果:抓取某个学校网站的教授名录,并获取研究方向。

由于网站使用的都是明文,所以抓起来没什么难度,且平时访问量小,很值得用来练习。

代码如下,解释请见注释

python 复制代码
import time

import requests
from bs4 import BeautifulSoup

# 创建一个包含浏览器头部信息的字典,模拟浏览器,可以骗过一些简单的反爬虫网站
headers = {
	'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36',
	# 可以根据需要添加更多的头部信息,比如:
	# 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
	# 'Accept-Language': 'en-US,en;q=0.5',
	# 'Accept-Encoding': 'gzip, deflate, br',
	# 'DNT': '1', # Do Not Track 请求头表明用户不希望被追踪
	# 'Connection': 'keep-alive',
	# 'Upgrade-Insecure-Requests': '1',
	# 如果需要处理cookies,也可以在这里设置'Cookie'字段
	# 'Cookie': 'your_cookie_here',
}


# 获取教师信息
def request_teacher_info(url):
	# 发送GET请求,并带上headers
	response = requests.get(url, headers=headers)
	content = response.content.decode('utf-8')

	# 确保请求成功
	if response.status_code == 200:
		soup = BeautifulSoup(content, 'html.parser')
		# 抓取div标签下,class为infobox的标签
		infobox_div = soup.find('div', class_='infobox')
		# 抓取span标签,class为'h_info',并且包含"姓名"字样
		name_span = infobox_div.find('span', class_='h_info', string='姓名:')
		# 抓取"姓名"字样后一个字样,即实际姓名
		name = name_span.find_next_sibling('span').get_text(strip=True)
		# 同样的方法抓研究方向,想继续抓,还有邮箱,发表论文这些,都是一样的套路
		research_direction_span = infobox_div.find('span', class_='h_info', string='研究方向:')
		research_direction = research_direction_span.find_next_sibling('span').get_text(strip=True)
		print(f"{name}  研究方向:{research_direction}")
	else:
		print(f"请求失败,状态码为:{response.status_code}")


# 获取教师列表
def request_teacher_list(url):
	# 发送GET请求,并带上headers
	response = requests.get(url, headers=headers)
	content = response.content.decode('utf-8')
	link_list = []
	# 确保请求成功
	if response.status_code == 200:
		# 使用BeautifulSoup解析HTML内容
		soup = BeautifulSoup(content, 'html.parser')

		right_list_r = soup.find('div', class_='right-list r')
		# 教师列表
		teacher_lists = right_list_r.find_all('div', class_='teacher-list')
		for teacher_list in teacher_lists:
			job_type = teacher_list.find("h2", class_="title l")
			# 这些打印信息可以忽略,重要信息已在request_teacher_info()中展示
			print(job_type.get_text(strip=True))
			professor_ul = teacher_list.find_all('ul')[0]
			a_list = professor_ul.find_all('a', href=True)

			for a in a_list:
				link = a['href']
				link_list.append(link)
				print(link)
			print("=" * 50)
	return link_list


link_list1 = request_teacher_list("https://example.com")
for link in link_list1:
	request_teacher_info(link)
	# time.sleep(0.5)
相关推荐
金銀銅鐵26 分钟前
[Python] 扩展欧几里得算法
python·数学·算法
Duckdblab41 分钟前
DuckDB 性能调优终极指南:打造闪电般的分析体验
python
带派擂总1 小时前
Python全栈开发精华版最全合集(包含各种面试题) Day24_异常和错误
python
金銀銅鐵4 小时前
n^5 和 n 的个位数是否总相等?
python·数学
aqi008 小时前
15天学会AI应用开发(九)利用Chroma持久化向量数据
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵8 小时前
借助 Pygame 探索最大公约数的规律
python·数学·游戏
ServBay1 天前
9 个 Python 第三方库推荐,不用 AI 都好像多出一个团队
后端·python
用户8356290780511 天前
如何使用 Python 添加和管理 Excel 批注(完整示例)
后端·python
用户8356290780511 天前
使用 Python 管理 Excel 工作表:创建、复制、删除与重命名
后端·python