Python100个库分享第37个—BeautifulSoup(爬虫篇)

目录

    • 专栏导读
    • 📚 库简介
      • 🎯 主要特点
    • 🛠️ 安装方法
    • 🚀 快速入门
      • 基本使用流程
      • 解析器选择
    • 🔍 核心功能详解
      • 1. 基本查找方法
        • find() 和 find_all()
        • CSS选择器
      • 2. 属性操作
      • 3. 文本提取
    • 🕷️ 实战爬虫案例
      • 案例1:爬取新闻标题和链接
      • 案例2:爬取商品信息
      • 案例3:爬取表格数据
    • 🛡️ 高级技巧与最佳实践
      • 1. 处理动态内容
      • 2. 数据清洗和验证
      • 3. 处理编码问题
      • 4. 并发爬取
    • ⚠️ 注意事项与最佳实践
      • 1. 遵守robots.txt
      • 2. 设置合理的延迟
      • 3. 错误处理和日志记录
    • 🔧 常见问题解决
      • 1. 处理JavaScript渲染的页面
      • 2. 处理反爬虫机制
    • 📊 性能优化
      • 1. 内存优化
      • 2. 缓存机制
    • 🎯 总结
      • ✅ 优点
      • ⚠️ 局限性
      • 🚀 最佳实践建议

专栏导读

  • 🌸 欢迎来到Python办公自动化专栏—Python处理办公问题,解放您的双手

  • 🏳️‍🌈 博客主页:请点击——> 一晌小贪欢的博客主页求关注

  • 👍 该系列文章专栏:请点击——>Python办公自动化专栏求订阅

  • 🕷 此外还有爬虫专栏:请点击——>Python爬虫基础专栏求订阅

  • 📕 此外还有python基础专栏:请点击——>Python基础学习专栏求订阅

  • 文章作者技术和水平有限,如果文中出现错误,希望大家能指正🙏

  • ❤️ 欢迎各位佬关注! ❤️

📚 库简介

  • BeautifulSoup是Python中最受欢迎的HTML和XML解析库之一,专门用于网页数据提取和网络爬虫开发。它提供了简单易用的API来解析HTML/XML文档,让开发者能够轻松地从网页中提取所需的数据。

🎯 主要特点

  • 简单易用:提供直观的API,即使是初学者也能快速上手
  • 强大的解析能力:支持多种解析器(html.parser、lxml、html5lib等)
  • 灵活的查找方式:支持CSS选择器、标签名、属性等多种查找方式
  • 容错性强:能够处理格式不规范的HTML文档
  • 与requests完美配合:是网络爬虫开发的黄金组合

🛠️ 安装方法

# 基础安装
pip install beautifulsoup4# 推荐安装(包含lxml解析器)
pip install beautifulsoup4 lxml# 完整安装(包含所有解析器)
pip install beautifulsoup4 lxml html5lib

🚀 快速入门

基本使用流程

from bs4 import BeautifulSoup
import requests# 1. 获取网页内容
url = "https://example.com"
response = requests.get(url)
html_content = response.text# 2. 创建BeautifulSoup对象
soup = BeautifulSoup(html_content, 'html.parser')# 3. 解析和提取数据
title = soup.find('title').text
print(f"网页标题: {title}")

解析器选择

from bs4 import BeautifulSouphtml = "<html><head><title>测试页面</title></head><body><p>Hello World</p></body></html>"# 不同解析器的使用
soup1 = BeautifulSoup(html, 'html.parser')    # Python内置解析器
soup2 = BeautifulSoup(html, 'lxml')           # lxml解析器(推荐)
soup3 = BeautifulSoup(html, 'html5lib')       # html5lib解析器

🔍 核心功能详解

1. 基本查找方法

find() 和 find_all()
from bs4 import BeautifulSouphtml = """
<html>
<body><div class="container"><h1 id="title">主标题</h1><p class="content">第一段内容</p><p class="content">第二段内容</p><a href="https://example.com">链接1</a><a href="https://test.com">链接2</a></div>
</body>
</html>
"""soup = BeautifulSoup(html, 'html.parser')# find() - 查找第一个匹配的元素
first_p = soup.find('p')
print(f"第一个p标签: {first_p.text}")# find_all() - 查找所有匹配的元素
all_p = soup.find_all('p')
for p in all_p:print(f"p标签内容: {p.text}")# 根据属性查找
title = soup.find('h1', id='title')
content_p = soup.find_all('p', class_='content')
CSS选择器
# 使用CSS选择器
soup = BeautifulSoup(html, 'html.parser')# select() - 返回列表
titles = soup.select('h1')
contents = soup.select('.content')
links = soup.select('a[href]')# select_one() - 返回第一个匹配元素
first_content = soup.select_one('.content')# 复杂选择器
nested_elements = soup.select('div.container p.content')

2. 属性操作

from bs4 import BeautifulSouphtml = '<a href="https://example.com" class="external" id="link1">示例链接</a>'
soup = BeautifulSoup(html, 'html.parser')link = soup.find('a')# 获取属性
href = link.get('href')
# 或者使用字典方式
href = link['href']# 获取所有属性
attrs = link.attrs
print(f"所有属性: {attrs}")# 检查属性是否存在
if link.has_attr('class'):print(f"class属性: {link['class']}")# 修改属性
link['href'] = 'https://newurl.com'
link['target'] = '_blank'

3. 文本提取

from bs4 import BeautifulSouphtml = """
<div><h1>标题</h1><p>这是一段<strong>重要</strong>的文本</p><ul><li>项目1</li><li>项目2</li></ul>
</div>
"""soup = BeautifulSoup(html, 'html.parser')# 获取文本内容
div = soup.find('div')# .text - 获取所有文本(包括子元素)
all_text = div.text
print(f"所有文本: {all_text}")# .get_text() - 更灵活的文本提取
clean_text = div.get_text(separator=' ', strip=True)
print(f"清理后的文本: {clean_text}")# .string - 只有当元素只包含一个字符串时才返回
p = soup.find('p')
print(f"p标签的string: {p.string}")  # None,因为包含子元素# .strings - 生成器,返回所有字符串
for string in div.strings:print(f"字符串: {repr(string)}")

🕷️ 实战爬虫案例

案例1:爬取新闻标题和链接

import requests
from bs4 import BeautifulSoup
import timedef crawl_news():"""爬取新闻网站的标题和链接"""# 设置请求头,模拟浏览器访问headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}try:# 发送请求url = "https://news.example.com"  # 替换为实际的新闻网站response = requests.get(url, headers=headers, timeout=10)response.raise_for_status()# 解析HTMLsoup = BeautifulSoup(response.text, 'html.parser')# 查找新闻标题(根据实际网站结构调整选择器)news_items = soup.find_all('div', class_='news-item')news_list = []for item in news_items:title_element = item.find('h3') or item.find('h2')link_element = item.find('a')if title_element and link_element:title = title_element.get_text(strip=True)link = link_element.get('href')# 处理相对链接if link.startswith('/'):link = f"https://news.example.com{link}"news_list.append({'title': title,'link': link})return news_listexcept requests.RequestException as e:print(f"请求错误: {e}")return []# 使用示例
if __name__ == "__main__":news = crawl_news()for item in news[:5]:  # 显示前5条新闻print(f"标题: {item['title']}")print(f"链接: {item['link']}")print("-" * 50)

案例2:爬取商品信息

import requests
from bs4 import BeautifulSoup
import json
import timeclass ProductCrawler:def __init__(self):self.session = requests.Session()self.session.headers.update({'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'})def crawl_product_list(self, category_url):"""爬取商品列表页面"""try:response = self.session.get(category_url, timeout=10)response.raise_for_status()soup = BeautifulSoup(response.text, 'html.parser')# 查找商品容器(根据实际网站调整)products = soup.find_all('div', class_='product-item')product_list = []for product in products:product_info = self.extract_product_info(product)if product_info:product_list.append(product_info)return product_listexcept Exception as e:print(f"爬取商品列表失败: {e}")return []def extract_product_info(self, product_element):"""提取单个商品信息"""try:# 商品名称name_element = product_element.find('h3', class_='product-name')name = name_element.get_text(strip=True) if name_element else "未知商品"# 价格price_element = product_element.find('span', class_='price')price = price_element.get_text(strip=True) if price_element else "价格未知"# 图片img_element = product_element.find('img')image_url = img_element.get('src') if img_element else ""# 商品链接link_element = product_element.find('a')product_url = link_element.get('href') if link_element else ""# 评分rating_element = product_element.find('span', class_='rating')rating = rating_element.get_text(strip=True) if rating_element else "无评分"return {'name': name,'price': price,'image_url': image_url,'product_url': product_url,'rating': rating}except Exception as e:print(f"提取商品信息失败: {e}")return Nonedef save_to_json(self, products, filename):"""保存数据到JSON文件"""try:with open(filename, 'w', encoding='utf-8') as f:json.dump(products, f, ensure_ascii=False, indent=2)print(f"数据已保存到 {filename}")except Exception as e:print(f"保存文件失败: {e}")# 使用示例
if __name__ == "__main__":crawler = ProductCrawler()# 爬取商品信息products = crawler.crawl_product_list("https://shop.example.com/category/electronics")# 保存数据if products:crawler.save_to_json(products, "products.json")print(f"共爬取到 {len(products)} 个商品")

案例3:爬取表格数据

import requests
from bs4 import BeautifulSoup
import pandas as pddef crawl_table_data(url, table_selector=None):"""爬取网页中的表格数据"""headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}try:response = requests.get(url, headers=headers, timeout=10)response.raise_for_status()soup = BeautifulSoup(response.text, 'html.parser')# 查找表格if table_selector:table = soup.select_one(table_selector)else:table = soup.find('table')if not table:print("未找到表格")return None# 提取表头headers_row = table.find('thead') or table.find('tr')headers = []if headers_row:for th in headers_row.find_all(['th', 'td']):headers.append(th.get_text(strip=True))# 提取数据行rows = []tbody = table.find('tbody')if tbody:data_rows = tbody.find_all('tr')else:data_rows = table.find_all('tr')[1:]  # 跳过表头行for row in data_rows:cells = row.find_all(['td', 'th'])row_data = []for cell in cells:# 处理单元格内容cell_text = cell.get_text(strip=True)row_data.append(cell_text)if row_data:  # 只添加非空行rows.append(row_data)# 创建DataFrameif headers and rows:# 确保所有行的列数一致max_cols = max(len(headers), max(len(row) for row in rows) if rows else 0)# 补齐表头while len(headers) < max_cols:headers.append(f"Column_{len(headers) + 1}")# 补齐数据行for row in rows:while len(row) < max_cols:row.append("")df = pd.DataFrame(rows, columns=headers[:max_cols])return dfreturn Noneexcept Exception as e:print(f"爬取表格数据失败: {e}")return None# 使用示例
if __name__ == "__main__":# 爬取表格数据url = "https://example.com/data-table"df = crawl_table_data(url)if df is not None:print("表格数据预览:")print(df.head())# 保存到CSVdf.to_csv("table_data.csv", index=False, encoding='utf-8-sig')print("数据已保存到 table_data.csv")

🛡️ 高级技巧与最佳实践

1. 处理动态内容

from bs4 import BeautifulSoup
import requests
import timedef crawl_with_retry(url, max_retries=3, delay=1):"""带重试机制的爬取函数"""headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}for attempt in range(max_retries):try:response = requests.get(url, headers=headers, timeout=10)response.raise_for_status()return response.textexcept requests.RequestException as e:print(f"第 {attempt + 1} 次尝试失败: {e}")if attempt < max_retries - 1:time.sleep(delay * (attempt + 1))  # 递增延迟else:raisereturn None

2. 数据清洗和验证

import re
from bs4 import BeautifulSoupclass DataCleaner:@staticmethoddef clean_text(text):"""清理文本数据"""if not text:return ""# 移除多余的空白字符text = re.sub(r'\s+', ' ', text.strip())# 移除特殊字符text = re.sub(r'[^\w\s\u4e00-\u9fff.,!?;:]', '', text)return text@staticmethoddef extract_price(price_text):"""提取价格数字"""if not price_text:return None# 使用正则表达式提取数字price_match = re.search(r'[\d,]+\.?\d*', price_text.replace(',', ''))if price_match:return float(price_match.group().replace(',', ''))return None@staticmethoddef validate_url(url):"""验证URL格式"""url_pattern = re.compile(r'^https?://'  # http:// or https://r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|'  # domain...r'localhost|'  # localhost...r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'  # ...or ipr'(?::\d+)?'  # optional portr'(?:/?|[/?]\S+)$', re.IGNORECASE)return url_pattern.match(url) is not None# 使用示例
cleaner = DataCleaner()# 清理文本
dirty_text = "  这是一段   包含多余空格的\n\n文本  "
clean_text = cleaner.clean_text(dirty_text)
print(f"清理后的文本: '{clean_text}'")# 提取价格
price_text = "¥1,299.99"
price = cleaner.extract_price(price_text)
print(f"提取的价格: {price}")

3. 处理编码问题

import requests
from bs4 import BeautifulSoup
import chardetdef smart_crawl(url):"""智能处理编码的爬取函数"""headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}try:response = requests.get(url, headers=headers, timeout=10)response.raise_for_status()# 检测编码detected_encoding = chardet.detect(response.content)encoding = detected_encoding['encoding']print(f"检测到的编码: {encoding}")# 使用检测到的编码解码if encoding:html_content = response.content.decode(encoding, errors='ignore')else:html_content = response.text# 创建BeautifulSoup对象soup = BeautifulSoup(html_content, 'html.parser')return soupexcept Exception as e:print(f"爬取失败: {e}")return None

4. 并发爬取

import asyncio
import aiohttp
from bs4 import BeautifulSoup
import timeclass AsyncCrawler:def __init__(self, max_concurrent=5):self.max_concurrent = max_concurrentself.semaphore = asyncio.Semaphore(max_concurrent)async def fetch_page(self, session, url):"""异步获取单个页面"""async with self.semaphore:try:async with session.get(url, timeout=10) as response:if response.status == 200:html = await response.text()return url, htmlelse:print(f"HTTP {response.status}: {url}")return url, Noneexcept Exception as e:print(f"获取页面失败 {url}: {e}")return url, Noneasync def crawl_multiple_pages(self, urls):"""并发爬取多个页面"""headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}async with aiohttp.ClientSession(headers=headers) as session:tasks = [self.fetch_page(session, url) for url in urls]results = await asyncio.gather(*tasks)return resultsdef parse_pages(self, results):"""解析爬取结果"""parsed_data = []for url, html in results:if html:soup = BeautifulSoup(html, 'html.parser')# 提取数据(根据实际需求调整)title = soup.find('title')title_text = title.get_text(strip=True) if title else "无标题"parsed_data.append({'url': url,'title': title_text,'content_length': len(html)})return parsed_data# 使用示例
async def main():urls = ["https://example1.com","https://example2.com","https://example3.com",# 添加更多URL]crawler = AsyncCrawler(max_concurrent=3)start_time = time.time()results = await crawler.crawl_multiple_pages(urls)parsed_data = crawler.parse_pages(results)end_time = time.time()print(f"爬取完成,耗时: {end_time - start_time:.2f}秒")print(f"成功爬取: {len(parsed_data)} 个页面")for data in parsed_data:print(f"URL: {data['url']}")print(f"标题: {data['title']}")print(f"内容长度: {data['content_length']}")print("-" * 50)# 运行异步爬虫
if __name__ == "__main__":asyncio.run(main())

⚠️ 注意事项与最佳实践

1. 遵守robots.txt

import urllib.robotparserdef check_robots_txt(url, user_agent='*'):"""检查robots.txt是否允许爬取"""try:rp = urllib.robotparser.RobotFileParser()rp.set_url(f"{url}/robots.txt")rp.read()return rp.can_fetch(user_agent, url)except:return True  # 如果无法获取robots.txt,默认允许# 使用示例
url = "https://example.com/page"
if check_robots_txt(url):print("允许爬取")
else:print("robots.txt禁止爬取")

2. 设置合理的延迟

import time
import randomclass RateLimiter:def __init__(self, min_delay=1, max_delay=3):self.min_delay = min_delayself.max_delay = max_delayself.last_request_time = 0def wait(self):"""等待适当的时间间隔"""current_time = time.time()elapsed = current_time - self.last_request_timedelay = random.uniform(self.min_delay, self.max_delay)if elapsed < delay:sleep_time = delay - elapsedtime.sleep(sleep_time)self.last_request_time = time.time()# 使用示例
rate_limiter = RateLimiter(min_delay=1, max_delay=3)for url in urls:rate_limiter.wait()  # 等待# 执行爬取操作response = requests.get(url)

3. 错误处理和日志记录

import logging
from datetime import datetime# 配置日志
logging.basicConfig(level=logging.INFO,format='%(asctime)s - %(levelname)s - %(message)s',handlers=[logging.FileHandler('crawler.log', encoding='utf-8'),logging.StreamHandler()]
)logger = logging.getLogger(__name__)class SafeCrawler:def __init__(self):self.success_count = 0self.error_count = 0def crawl_with_logging(self, url):"""带日志记录的爬取函数"""try:logger.info(f"开始爬取: {url}")response = requests.get(url, timeout=10)response.raise_for_status()soup = BeautifulSoup(response.text, 'html.parser')self.success_count += 1logger.info(f"爬取成功: {url}")return soupexcept requests.exceptions.Timeout:self.error_count += 1logger.error(f"请求超时: {url}")except requests.exceptions.HTTPError as e:self.error_count += 1logger.error(f"HTTP错误 {e.response.status_code}: {url}")except Exception as e:self.error_count += 1logger.error(f"未知错误: {url} - {str(e)}")return Nonedef get_stats(self):"""获取爬取统计信息"""total = self.success_count + self.error_countsuccess_rate = (self.success_count / total * 100) if total > 0 else 0return {'total': total,'success': self.success_count,'errors': self.error_count,'success_rate': f"{success_rate:.2f}%"}

🔧 常见问题解决

1. 处理JavaScript渲染的页面

# 对于JavaScript渲染的页面,BeautifulSoup无法直接处理
# 需要配合Selenium使用from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoupdef crawl_js_page(url):"""爬取JavaScript渲染的页面"""# 配置Chrome选项chrome_options = Options()chrome_options.add_argument('--headless')  # 无头模式chrome_options.add_argument('--no-sandbox')chrome_options.add_argument('--disable-dev-shm-usage')try:driver = webdriver.Chrome(options=chrome_options)driver.get(url)# 等待页面加载time.sleep(3)# 获取渲染后的HTMLhtml = driver.page_source# 使用BeautifulSoup解析soup = BeautifulSoup(html, 'html.parser')return soupfinally:driver.quit()

2. 处理反爬虫机制

import random
import timeclass AntiAntiCrawler:def __init__(self):self.user_agents = ['Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36']self.session = requests.Session()def get_random_headers(self):"""获取随机请求头"""return {'User-Agent': random.choice(self.user_agents),'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3','Accept-Encoding': 'gzip, deflate','Connection': 'keep-alive',}def crawl_with_proxy(self, url, proxy=None):"""使用代理爬取"""headers = self.get_random_headers()proxies = {'http': proxy, 'https': proxy} if proxy else Nonetry:response = self.session.get(url, headers=headers, proxies=proxies,timeout=10)return response.textexcept Exception as e:print(f"爬取失败: {e}")return None

📊 性能优化

1. 内存优化

from bs4 import BeautifulSoup
import gcdef memory_efficient_crawl(urls):"""内存高效的爬取方法"""for url in urls:try:response = requests.get(url, stream=True)# 分块读取大文件content = ""for chunk in response.iter_content(chunk_size=8192, decode_unicode=True):content += chunksoup = BeautifulSoup(content, 'html.parser')# 处理数据process_page(soup)# 清理内存del soupdel contentgc.collect()except Exception as e:print(f"处理 {url} 时出错: {e}")def process_page(soup):"""处理页面数据"""# 只提取需要的数据,避免保存整个soup对象title = soup.find('title')if title:print(f"标题: {title.get_text(strip=True)}")

2. 缓存机制

import pickle
import os
from datetime import datetime, timedeltaclass CrawlerCache:def __init__(self, cache_dir='cache', expire_hours=24):self.cache_dir = cache_dirself.expire_hours = expire_hoursif not os.path.exists(cache_dir):os.makedirs(cache_dir)def get_cache_path(self, url):"""获取缓存文件路径"""import hashliburl_hash = hashlib.md5(url.encode()).hexdigest()return os.path.join(self.cache_dir, f"{url_hash}.cache")def is_cache_valid(self, cache_path):"""检查缓存是否有效"""if not os.path.exists(cache_path):return Falsecache_time = datetime.fromtimestamp(os.path.getmtime(cache_path))expire_time = datetime.now() - timedelta(hours=self.expire_hours)return cache_time > expire_timedef get_cached_content(self, url):"""获取缓存内容"""cache_path = self.get_cache_path(url)if self.is_cache_valid(cache_path):try:with open(cache_path, 'rb') as f:return pickle.load(f)except:passreturn Nonedef save_to_cache(self, url, content):"""保存到缓存"""cache_path = self.get_cache_path(url)try:with open(cache_path, 'wb') as f:pickle.dump(content, f)except Exception as e:print(f"保存缓存失败: {e}")# 使用示例
cache = CrawlerCache()def crawl_with_cache(url):"""带缓存的爬取函数"""# 尝试从缓存获取cached_content = cache.get_cached_content(url)if cached_content:print(f"使用缓存: {url}")return BeautifulSoup(cached_content, 'html.parser')# 从网络获取try:response = requests.get(url, timeout=10)response.raise_for_status()# 保存到缓存cache.save_to_cache(url, response.text)return BeautifulSoup(response.text, 'html.parser')except Exception as e:print(f"爬取失败: {e}")return None

🎯 总结

BeautifulSoup是Python爬虫开发中不可或缺的工具,它的优势在于:

✅ 优点

  • 简单易学:API设计直观,学习曲线平缓
  • 功能强大:支持多种查找方式和解析器
  • 容错性好:能处理格式不规范的HTML
  • 文档完善:官方文档详细,社区活跃

⚠️ 局限性

  • 不支持JavaScript:无法处理动态渲染的内容
  • 性能相对较慢:相比lxml等纯C库性能较低
  • 内存占用:解析大文件时内存占用较高

🚀 最佳实践建议

  1. 选择合适的解析器:推荐使用lxml解析器
  2. 遵守网站规则:检查robots.txt,设置合理延迟
  3. 错误处理:完善的异常处理和重试机制
  4. 数据清洗:对提取的数据进行验证和清理
  5. 性能优化:使用缓存、并发等技术提高效率
  • BeautifulSoup配合requests库,是Python爬虫开发的经典组合,适合大多数网页数据提取任务。掌握这个库,将为你的数据采集工作提供强大的支持!

  • 希望对初学者有帮助;致力于办公自动化的小小程序员一枚

  • 希望能得到大家的【❤️一个免费关注❤️】感谢!

  • 求个 🤞 关注 🤞 +❤️ 喜欢 ❤️ +👍 收藏 👍

  • 此外还有办公自动化专栏,欢迎大家订阅:Python办公自动化专栏

  • 此外还有爬虫专栏,欢迎大家订阅:Python爬虫基础专栏

  • 此外还有Python基础专栏,欢迎大家订阅:Python基础学习专栏

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/news/915535.shtml
繁体地址,请注明出处:http://hk.pswp.cn/news/915535.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

石子入水波纹效果:顶点扰动着色器实现

水面波纹的真实模拟是计算机图形学中一个经典且重要的课题,广泛应用于游戏、影视和虚拟现实等领域。本文将从技术原理和实现细节出发,系统介绍如何利用**顶点扰动(Vertex Displacement)**技术,结合多种辅助方法,打造既真实又高效的水面波纹效果。 一、顶点扰动的核心思想…

【FFmpeg 快速入门】本地播放器 项目

目录 &#x1f308;前言&#x1f308; &#x1f4c1; 整体架构 详细流程 &#x1f4c1; 数据流向​ &#x1f4c1; 队列设计​编辑 &#x1f4c1; 线程设计 &#x1f4c1; 音视频同步 &#x1f4c1; 音频输出设计 &#x1f4c1; 视频输出设计 &#x1f4c1; 总结 …

Maven dependencyManagement标签 properties标签

dependencyManagement标签properties标签

前端埋坑之element Ui 组件el-progress display:flex后不显示进度条解决方案

项目适用场景&#xff1a; <divs style"display&#xff1a;flex"> <span>这里是进度条前标题说明</span> <el-progress :percentage"50"></el-progress> </div> 问题呈现&#xff1a; el-progress进度条没啦&#xf…

嵌入式学习-土堆PyTorch(7)-day23

损失函数的调用import torch from torch import nn from torch.nn import L1Lossinputs torch.tensor([1.0,2.0,3.0]) target torch.tensor([1.0,2.0,5.0])inputs torch.reshape(inputs, (1, 1, 1, 3)) target torch.reshape(target, (1, 1, 1, 3)) #损失函数 loss L1Loss…

【2025最新】使用neo4j实现GraphRAG所需的向量检索

学习笔记&#xff0c;比较混乱&#xff0c;介意慎点。 背景 在将UMLS或者LightRAG构造的数据库存入neo4j之后&#xff0c;我开始将知识图谱运用到实际场景的使用中、例如查询、推理。然而&#xff0c;由于字符串匹配导致大量术语在检索时出现缺失。导致检索效果不佳。我们需要…

【AI深究】随机森林(Random Forest)全网最详细全流程详解与案例(附Python代码演示)|集成学习|数学原理、案例流程、代码演示及结果解读|参数与调优、工程启示、单棵决策树的对比、优缺点

大家好&#xff0c;我是爱酱。本篇将会系统地讲解随机森林&#xff08;Random Forest&#xff09;的原理、核心思想、数学表达、算法流程、代码实现与工程应用。内容适合初学者和进阶读者&#xff0c;配合公式和可视化示例。 注&#xff1a;本文章含大量数学算式、详细例子说明…

6.String、StringBuffer、StringBuilder区别及使用场景

String固定长度&#xff0c;引用的字符串内容无法改变例如&#xff0c;String s abc;s def;那么 s 字符串对应的地址已经改变了StringBuider 声明一个对象时&#xff0c;指向堆中的一块空间&#xff0c;包括两个属性 value 和 count其中 value 属性类似动态数组&#xff0c;可…

Qualcomm Linux 蓝牙指南学习--验证 Fluoride 协议栈的功能(2)

前言 Qualcomm Technologies推出的Linux蓝牙指南详细介绍了基于Qualcomm RB3 Gen 2和IQ-9100 Beta开发套件的蓝牙解决方案。该文档涵盖BlueZ和Fluoride协议栈的功能验证流程,支持蓝牙5.2核心规范,包括WCN6750/WCN6856/QCA6698AQ芯片组的特性。主要内容分为三部分: ‌1. 功…

Spring Boot中REST与gRPC并存架构设计与性能优化实践指南

Spring Boot中REST与gRPC并存架构设计与性能优化实践指南 在微服务架构日益复杂的当下&#xff0c;单一协议往往难以满足高并发低延迟与生态兼容的双重需求。本文基于真实生产环境的项目经验&#xff0c;分享了如何在Spring Boot中同时提供RESTful API和gRPC接口的架构设计、性…

Simulink仿真-model Setting关键配置

1.概述 Simulink 的仿真设置&#xff08;Model Settings&#xff09;是确保仿真准确性的关键配置&#xff0c;主要包括仿真时间、步长、解法器选择等核心参数的设定。 ‌可以通过快捷键‌CtrlE‌打开仿真设置界面2.核心参数 2.1 求解器Solver配置 时间范围&#xff1a;设置仿真…

内网与外网是通过什么进行传输的?内外网文件传输的安全方法

在当前企业信息化建设日益深入的背景下&#xff0c;出于安全防护与合规管理的需要&#xff0c;很多单位将网络划分为内网&#xff08;办公网/生产网&#xff09;与外网&#xff08;互联网/DMZ区&#xff09;。这种网络隔离策略虽然提升了安全性&#xff0c;但也带来了内外网文件…

RabbitMQ面试精讲 Day 4:Queue属性与消息特性

【RabbitMQ面试精讲 Day 4】Queue属性与消息特性 开篇 欢迎来到"RabbitMQ面试精讲"系列的第4天&#xff01;今天我们将深入探讨RabbitMQ中Queue的属性配置与消息特性&#xff0c;这是理解和优化RabbitMQ使用的关键知识点。掌握这些内容不仅能帮助你在面试中展现深厚…

uniapp vue3 vite项目使用微信云开发(云函数)

1、在根目录新建文件夹 cloudfunctions2、配置 manifest.json在项目根目录的 manifest.json 文件中&#xff0c;找到微信小程序配置部分&#xff0c;添加 cloudfunctionRoot 字段&#xff0c;指向你的云函数目录&#xff1a;{"mp-weixin": {"cloudfunctionRoot&…

AK视频下载工具:免费高效,多平台支持

近期小编又发现了一款更强大的新神器——AK视频下载&#xff08;电脑版&#xff09;&#xff0c;一起来了解下吧~ 软件亮点 完全免费&#xff0c;无需安装&#xff0c;操作便捷&#xff0c;直接打开即可使用。 支持多平台视频下载&#xff0c;包括抖音、B站、小红书、快手等主…

7月21日星期一今日早报简报微语报早读

7月21日星期一&#xff0c;农历六月廿七&#xff0c;早报#微语早读。1、广东佛山累计报告基孔肯雅热确诊病例1873例&#xff0c;均为轻症&#xff1b;2、祝贺&#xff01;石宇奇夺得日本羽毛球公开赛男单冠军&#xff1b;3、中国和匈牙利顺利完成引渡条约谈判&#xff1b;4、科…

基于Milvus Lite的轻量级向量数据库实战指南

一、为什么选择Milvus Lite&#xff1f; 在人工智能和语义搜索应用中&#xff0c;高效的向量检索是核心需求。相比需要部署Docker的完整版Milvus&#xff0c;Milvus Lite提供&#xff1a; 零依赖&#xff1a;纯Python实现&#xff0c;无需安装Docker或外部组件 开箱即用&…

深度学习时代下的社交媒体情感分析:方法、工具与未来挑战

摘要&#xff1a;基于Yue等学者2019年发表的权威综述&#xff0c;本文系统总结情感分析的技术框架、实战资源与前沿方向&#xff0c;附Python代码示例。 一、情感分析为何重要&#xff1f; 情感分析&#xff08;Sentiment Analysis&#xff09;旨在从文本中提取主观态度&…

Spring Boot 3.0新特性全面解析与实战应用

Spring Boot 3.0新特性全面解析与实战应用 引言 Spring Boot 3.0作为Spring生态系统的一个重要里程碑&#xff0c;带来了众多令人兴奋的新特性和改进。本文将深入解析Spring Boot 3.0的核心变化&#xff0c;并通过实战示例展示如何在项目中应用这些新特性。 核心变化概览 Java版…

C# sqlsugar 主子表 联合显示 LeftJoin

在C#中使用SqlSugar ORM进行Left Join操作是一种常见需求&#xff0c;尤其是在处理复杂数据库查询时。SqlSugar是一个轻量级、高性能的ORM框架&#xff0c;支持多种数据库。下面是如何使用SqlSugar进行Left Join操作的示例。1. 安装SqlSugar首先&#xff0c;确保你的项目中已经…