# Python爬虫程序示例
import requests
from bs4 import BeautifulSoup
# 目标URL
url = 'https://example.com'
# 发送HTTP请求
response = requests.get(url)
# 检查请求是否成功
if response.status_code == 200:
# 解析HTML内容
soup = BeautifulSoup(response.text, 'html.parser')
# 查找所有标题标签<h1>
titles = soup.find_all('h1')
# 打印所有标题
for title in titles:
print(title.get_text())
else:
print("请求失败,状态码:", response.status_code)
requests
库来发送HTTP请求,并使用 BeautifulSoup
来解析HTML内容。requests.get()
方法发送GET请求。BeautifulSoup
解析返回的HTML内容。find_all()
方法查找所有的 <h1>
标签,并打印其文本内容。如果需要更复杂的爬虫功能或处理更多细节,请根据具体需求进行调整。
上一篇:pythoncount函数的用法
下一篇:python 运行时间
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站