【网络爬虫入门04】彻底掌握BeautifulSoup的CSS选择器

【网络爬虫入门04】彻底掌握BeautifulSoup的CSS选择器

广东职业技术学院  欧浩源 2017-10-21

1、引言

    目前,除了官方文档之外,市面上及网络详细介绍BeautifulSoup使用的技术书籍和博客软文并不多,而在这仅有的资料中介绍CSS选择器的少之又少。在网络爬虫的页面解析中,CCS选择器实际上是一把效率甚高的利器。虽然资料不多,但官方文档却十分详细,然而美中不足的是需要一定的基础才能看懂,而且没有小而精的演示实例。不过,在本文中,你可以看到......绝对的干活!

2、CSS选择器概述

    BeautifulSoup支持大部分的CSS选择器。
    语法为:向tag对象或BeautifulSoup对象的.select()方法中传入字符串参数,选择的结果以列表形式返回,即返回类型为list。
    tag.select("string")
    BeautifulSoup.select("string")
    注意:在取得含有特定CSS属性的元素时,标签名不加任何修饰,类名前加点,id名前加 #。

3、CSS测试样例

4、通过标签查找

例1:选择所有的title标签。

例2:选择所有的p标签中的第3个标签。

例3:选择body标签下的所有a标签。

例4:选择body标签下的直接子标签a。

例5:选择id=link1后的所有兄弟节点标签。类名前加点,id名前加 #。

例6:选择id=link1后的下一个兄弟节点标签。

5、通过CSS类名查找

例7:查找class类名为sister的标签。

例8:查找P标签下class类名为title的标签。

6、通过标签的id属性查找

例9:选择id属性为link2的所有标签。

例10:选择a标签,其id属性为link2的标签。

7、同时用多种CSS选择器查询元素

例11:选择id属性为link2和id属性为link3的所有标签。

例12:选择class属性为red、id属性为link2和id属性为link3的所有标签。

8、通过是否存在某个属性来查找

例13:查找a标签下存在herf属性的标签。

9、通过属性的值来查找

例14:选择a标签,其属性href=http://example.com/lacie的所有标签。

例15:选择a标签,其href属性以http开头的所有标签。

例16:选择a标签,其href属性以lie结尾的所有标签。

例17:选择a标签,其href属性包含.com的标签。

10、通过标签逐层查找

例18:首先选中所有的p标签中的第3个标签,然后在该标签中查找name的属性值为OHY的标签。

例19:首先选中所有的p标签中的第3个标签,然后在该标签列表中查找a标签,并将该列表中的第1个标签的文本取出。

11、返回查找到的元素的第一个标签

例20:选择class类名为sister的所有标签中的第一个。

12、小结

    如果你想快速的实现功能更强大的网络爬虫,那么BeautifulSoupCSS选择器将是你必备的利器之一。BeautifulSoup整合了CSS选择器的语法和自身方便使用API。在网络爬虫的开发过程中,对于熟悉CSS选择器语法的人,使用CSS选择器是个非常方便的方法。

转载于:https://www.cnblogs.com/ALittleBee/p/7702560.html

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

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

相关文章

1215B. The Number of Products

B. The Number of Products&#xff1a;题目 #include <bits/stdc.h> using namespace std; #define int long long vector<int> a((int)6e5); signed main() {int n;cin>>n;for (int i1;i<n;i) cin>>a[i];vector<int>dp(3);dp[1] dp[2] 0…

Mybatis基于XML配置SQL映射器(一)

Durid和Mybatis开发环境搭建 SpringBoot搭建基于SpringSpringMvcMybatis的REST服务&#xff08;http://www.cnblogs.com/nbfujx/p/7694768.html&#xff09; Mybatis之代码生成器 Maven Plugin管理 1 <build>2 <plugins>3 <plugin>4 …

1282B1. K for the Price of One (Easy Version)

B1. K for the Price of One (Easy Version)&#xff1a;题目 两种情况&#xff0c;前面取一或者前面不取#include <bits/stdc.h> using namespace std; #define int long long vector<int> a((int)6e5); vector<int> b((int)6e5), c((int)6e5); const int …

GIT的Push和Pull,强制Pull覆盖本地命令

连接命令&#xff1a; git remote add origin 你Git库的地址 其中&#xff0c;origin是你对这个Git库地址的标识。 一. 把文件从本地上传到库中 第一步&#xff1a;使用命令 git add命令把文件添加到暂存区里面去。git add * 命令是将当前文件夹内所有文件均添加到暂存区。 举…

1284B. New Year and Ascent Sequence

B. New Year and Ascent Sequence&#xff1a;题目 小小思维题#include <bits/stdc.h> using namespace std; #define int long long vector<int> a((int)6e5); vector<int> b((int)6e5), c((int)6e5); const int mod 1e9 7; signed main() {int t;cin>…

linux运维、架构之路-Zabbix监控

一、监控常用命令 1、物理服务器监控命令 ①添加yum源 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo ②安装 yum -y install OpenIPMI ipmitoo…

505B. Mr. Kitayuta‘s Colorful Graph

B. Mr. Kitayuta’s Colorful Graph&#xff1a;题目 一开始就像到了DFS&#xff0c;并查集也不难想到。 弗洛伊德算法倒是不容易想到&#xff0c;平时不这么用。。。。。#include <bits/stdc.h> using namespace std; #define int long long // vector<int> a((i…

vi命令

vi命令是UNIX操作系统和类UNIX操作系统中最通用的全屏幕纯文本编辑器。 Linux中的vi编辑器叫vim&#xff0c;它是vi的增强版&#xff08;vi Improved&#xff09;&#xff0c;与vi编辑器完全兼容&#xff0c;而且实现了很多增强功能。 vi编辑器支持编辑模式和命令模式&#xff…

1189C. Candies

C. Candies&#xff1a;题目 思维题&#xff0c;我是笨蛋。我看了半天也没想明白怎么dp&#xff0c;可恶啊。#include <bits/stdc.h> using namespace std; #define int long long vector<int> a((int)6e5); vector<int> b((int)6e5), c((int)6e5); const i…

SQL基本语法

SQL 基本语法 表库操作 创建表create table 表名(字段名 类型(长度) 约束,字段名 类型(长度) 约束); 创建主键约束id int primary key, primary key(id) // 在创建表最后创建主键约束 删除主键alter table 表名 drop primary key; 主键自动增长主键字段后加auto_increment(只适…

136. Single Number

Given an array of integers, every element appears twice except for one. Find that single one. 题目含义&#xff1a;给定的数组中&#xff0c;每个数字出现两次&#xff0c;只有一个数字出现了一次&#xff0c;找出这个数字 1 public int singleNumber(int[] nums) {…

797B. Odd sum

B. Odd sum&#xff1a;题目 一道小小的贪心#include <bits/stdc.h> using namespace std; #define int long long vector<int> a((int)6e5); vector<int> b((int)6e5), c[(int)6e5]; const int mod 1e9 7; string s; multiset<int> se; signed mai…

602B. Approximating a Constant Range

B. Approximating a Constant Range&#xff1a;题目 可恶啊&#xff0c;不知道为什么我双指针的代码不过&#xff0c;重新写了个。。。。。#include <bits/stdc.h> using namespace std; #define int long long vector<int> a((int)6e5); vector<int> b((i…

1415C. Bouncing Ball

C. Bouncing Ball&#xff1a;题目 很好的dp&#xff0c;1400分难见的题#include <bits/stdc.h> using namespace std; #define int long long vector<int> a((int)6e5); vector<int> b((int)6e5), c[(int)6e5]; const int mod 1e9 7; string s; signed m…

几种直播流媒体协议

题外话&#xff1a; HTTP渐进下载流媒体播放: 基于TCP。 yy、乐视、爱奇艺、优酷土豆、搜狐视频、花椒直播&#xff0c;主要还是通过rtmp&hls来实现的&#xff0c; 但他们也意识到rtmp的天生缺陷&#xff0c;所以不管是技术预研也好&#xff0c;还是测试版也好&#xff0c…

253B. Physics Practical

B. Physics Practical&#xff1a;题目 太简单的题目了&#xff0c;和dp没啥关系#include <bits/stdc.h> using namespace std; #define int long long vector<int> a((int)6e5); vector<int> b((int)6e5), c[(int)6e5]; const int mod 1e9 7; signed mai…

程序常识

闰年判定&#xff08;三种情况&#xff1a;或&#xff09;&#xff1a; 非整百年&#xff1a;能够被4整除&#xff0c;且不能被100整除&#xff08;year % 4 0 && year % 100 ! 0&#xff09;。整百年&#xff1a;能够被400整除&#xff08;year % 400 0&#xff09;…

940B. Our Tanya is Crying Out Loud

B. Our Tanya is Crying Out Loud&#xff1b;题目 1特判&#xff0c;因为除1还是本身&#xff0c;这点就很淦#include <bits/stdc.h> using namespace std; #define int long long vector<int> a((int)6e5); vector<int> b((int)6e5), c[(int)6e5]; const …

548B. Mike and Fun

B. Mike and Fun&#xff1a;题目 太离谱了呀&#xff0c;这啥遍历题啊。。。#include <bits/stdc.h> using namespace std; #define int long long vector<int> a((int)6e5); vector<int> b((int)6e5), c[(int)6e5]; const int mod 1e9 7; int g[1001][1…

EntityFramwork常见问题

1.常用的语句有哪些 添加migration dotnet ef migrations add [MIgrationName] 删除刚添加的migration dotnet ef migrations remove 更新数据库 dotnet ef database update、 2.在代码中用 attribute (code first) 限制表中字段的类型及主外键 主键 [Key]自…