Python爬虫教程-25-数据提取-BeautifulSoup4(三)

Python爬虫教程-25-数据提取-BeautifulSoup4(三)

本篇介绍 BeautifulSoup 中的 css 选择器

css 选择器

  • 使用 soup.select 返回一个列表
  • 通过标签名称:soup.select("title")
  • 通过类名:soup.select(".centent")
  • id 查找:soup.select("#name_id")
  • 组合查找:soup.select("div #input_content")
  • 属性查找:soup.select("img[class='photo']")
  • 获取tag内容:tag.get_text

案例

# BeautifulSoup 的使用案例
# css 选择器

from urllib import request
from bs4 import BeautifulSoup


url = 'http://www.baidu.com/'

rsp = request.urlopen(url)
content = rsp.read()

soup = BeautifulSoup(content, 'lxml')

# bs 自动解码
content = soup.prettify()

print("=="*12)
titles = soup.select("title")
print(titles[0])

print("=="*12)
meta = soup.select("meta[content='always']")
print(meta[0])

运行结果

这里写图片描述

更多文章链接:Python 爬虫随笔


- 本笔记不允许任何个人和组织转载
posted @   xpwi  阅读(719)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
阅读排行:
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· DeepSeek本地性能调优
· 一文掌握DeepSeek本地部署+Page Assist浏览器插件+C#接口调用+局域网访问!全攻略
点击右上角即可分享
微信分享提示

目录导航