import requests
from bs4 import BeautifulSoup
# 定义一个简单的网页爬虫函数
def simple_web_crawler(url):
try:
# 发送HTTP请求获取网页内容
response = requests.get(url)
response.raise_for_status() # 检查请求是否成功
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(response.text, 'html.parser')
# 示例:提取所有的标题标签<h1>
titles = soup.find_all('h1')
for title in titles:
print(title.get_text())
except requests.RequestException as e:
print(f"请求出错: {e}")
# 示例URL
url = "https://example.com"
simple_web_crawler(url)
导入库:
requests:用于发送HTTP请求。BeautifulSoup:用于解析HTML文档。定义函数 simple_web_crawler:
requests.get()发送HTTP GET请求并获取网页内容。BeautifulSoup解析HTML文档。<h1>标签并打印其文本内容。异常处理:
try-except块捕获可能的网络请求错误,并输出错误信息。示例调用:
"https://example.com" 调用爬虫函数。上一篇:python中find函数怎么用
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站