import requests
from bs4 import BeautifulSoup
# 定义一个函数来爬取网页数据
def fetch_webpage_data(url):
# 发送HTTP请求获取网页内容
response = requests.get(url)
# 检查请求是否成功
if response.status_code == 200:
# 使用BeautifulSoup解析HTML内容
soup = BeautifulSoup(response.content, 'html.parser')
# 示例:提取所有的标题标签<h1>
titles = soup.find_all('h1')
# 打印所有标题文本
for title in titles:
print(title.get_text())
else:
print("请求失败,状态码:", response.status_code)
# 示例URL
url = "https://example.com"
# 调用函数并传入URL
fetch_webpage_data(url)
导入库:
requests:用于发送HTTP请求。BeautifulSoup:用于解析HTML内容。定义函数 fetch_webpage_data:
requests.get()发送HTTP GET请求,获取网页内容。BeautifulSoup解析HTML内容。<h1>标签,并打印其文本内容。示例URL:
url = "https://example.com" 是一个示例URL,你可以替换为你想要爬取的网页地址。调用函数:
fetch_webpage_data(url)来执行爬取操作。这个代码示例展示了如何使用Python爬取网页数据并解析其中的内容。
上一篇:python中的int函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站