css选择器:

我们在写 CSS 时,标签名不加任何修饰,类名前加点,id名前加 #,在这里我们也可以利用类似的方法来筛选元素,用到的方法是 soup.select(),返回类型是 list

1)通过标签名查找

print soup.select('title')

#[<title>The Dormouse's story</title>]

print soup.select('a')

#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

2)通过类名查找

print soup.select('.sister')

#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

3)通过id名查找

print soup.select('#link1')
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]

4)组合查找

组合查找即和写 class 文件时,标签名与类名、id名进行的组合原理是一样的,例如查找 p 标签中,id 等于 link1的内容,二者需要用空格分开

print soup.select('p #link1')
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]

直接子标签查找

print soup.select("head>title")

#[<title>The Dormouse's story</title>]

5、属性查找

查找时还可以加入属性元素,属性需要用中括号括起来,注意属性和标签属于同一节点,所以中间不能加空格,否则会无法匹配到。

print soup.select('a[class="sister"]')
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

print soup.select('a[class="sister"]')
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

print soup.select('a[href="http://example.com/elsie"]')
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]

print soup.select('a[href="http://example.com/elsie"]')
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]
同样,属性仍然可以与上述查找方式组合,不在同一节点的空格隔开,同一节点的不加空格

print soup.select('p a[href="http://example.com/elsie"]')
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]

print soup.select('p a[href="http://example.com/elsie"]')
#[<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>]

以上的 select 方法返回的结果都是列表形式,可以遍历形式输出,然后用 get_text() 方法来获取它的内容。

soup = BeautifulSoup(html, 'lxml')
print type(soup.select('title'))
print soup.select('title')[0].get_text()

for title in soup.select('title'):
print title.get_text()

soup = BeautifulSoup(html, 'lxml')
print type(soup.select('title'))
print soup.select('title')[0].get_text()

for title in soup.select('title'):
print title.get_text()

好,这就是另一种与 find_all 方法有异曲同工之妙的查找方法,是不是感觉很方便?

【实战练习】:

爬取kugou top 500排名、歌手、歌曲、时间

#-*-coding:utf-8-*-
import requests
from bs4 import BeautifulSoup
import time
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) \
Chrome/66.0.3359.181 Safari/537.36 XiaoBai/10.0.1708.542 (XBCEF)'}
def get_info(url):
wb_data = requests.get(url,headers=headers)
soup = BeautifulSoup(wb_data.text,'lxml')
ranks = soup.select('span.pc_temp_num')                                  #获取排名情况
titles = soup.select('div.pc_temp_songlist > ul > li > a')              #获取歌曲标题
times = soup.select('span.pc_temp_tips_r > span')                   #获取歌曲时间
for rank,title,time in zip(ranks,titles,times):
data = {
'rank':rank.get_text().strip(),
'singer':title.get_text().split('-')[0],
'song':title.get_text().split('-')[-1],
'time':time.get_text().strip()
}
print (data)
if __name__ == '__main__':
urls = ['http://www.kugou.com/yy/rank/home/{}-8888.html'.format(number) for number in range(1,24)]           #构造多页url
for url in urls:
get_info(url)                                                                  #循环调用get_info函数
time.sleep(1)

转载于:https://www.cnblogs.com/yu2000/p/6861620.html

爬虫库之BeautifulSoup学习(五)相关推荐

  1. 第7课: bs4 库 的 BeautifulSoup 基础学习

    这里写目录标题 本节课内容所需要安装的 库: BeautifulSoup 简介: lxml 简介: requests ,BeautifulSoup 和 lxml 相互三者关系: 如何利用 bs4 的 ...

  2. python tag对象下有多个标签、属性_Python爬虫库BeautifulSoup获取对象(标签)名,属性,内容,注释...

    Apple iPhone 11 (A2223) 128GB 黑色 移动联通电信4G手机 双卡双待 4999元包邮 去购买 > 如何利用Python爬虫库BeautifulSoup获取对象(标签) ...

  3. [EntLib]微软企业库5.0 学习之路——第五步、介绍EntLib.Validation模块信息、验证器的实现层级及内置的各种验证器的使用方法——上篇...

    本文是为后面的学习之路做铺垫,简单介绍下企业库中的Validation模块的一些相关知识,包括Validation模块的简介.用途.使用方法.默认提供的多种验证器的介绍等. 一.简介及用途 在实际的项 ...

  4. python爬虫提取a标签_Python爬虫库BeautifulSoup获取对象(标签)名,属性,内容,注释

    一.Tag(标签)对象 1.Tag对象与XML或HTML原生文档中的tag相同. from bs4 import BeautifulSoup soup = BeautifulSoup('Extreme ...

  5. colly爬虫库学习笔记

    colly爬虫库学习笔记 前言 稍微的学习了一下Go语言的基础知识(错误处理和协程通道这些还没看),想着能不能做点东西,突然想到自己当时学了python之后就是专门为了写爬虫(虽然后来也咕了,只会一个 ...

  6. python获取标签属性值_Python爬虫库BeautifulSoup获取对象(标签)名,属性,内容,注释

    更多python教程请到: 菜鸟教程www.piaodoo.com 人人影视www.sfkyty.com 16影视www.591319.com 星辰影院www.591319.com 一.Tag(标签) ...

  7. Python3爬虫入门之beautifulsoup库的使用

    强调内容 BeautifulSoup 灵活又方便的网页解析库,处理高效,支持多种解析器.利用它不用编写正则表达式即可方便地实现网页信息的提取. 解析库 解析器 使用方法 优势 劣势 Python标准库 ...

  8. python爬虫和数据分析电脑推荐_大数据分析必备的5款Python爬虫库

    在数据科学或人工智能领域,除了算法之外,最重要的应该是数据了.甚至可以说一个模型到最后决定其准确度的往往不是算法而是数据.在现实中,缺少足够的数据成了数据分析师获得优秀模型的主要阻碍.可喜的是,现在网 ...

  9. Python爬虫:用BeautifulSoup进行NBA数据爬取

    爬虫主要就是要过滤掉网页中没用的信息.抓取网页中实用的信息 一般的爬虫架构为: 在python爬虫之前先要对网页的结构知识有一定的了解.如网页的标签,网页的语言等知识,推荐去W3School: W3s ...

最新文章

  1. C++中的string::compare的使用
  2. 面试官问:为什么SpringBoot的 jar 可以直接运行?
  3. 可视化应用实战案例:绘制交互式+pdf+png等多格式桑基图
  4. nginx 配置详解
  5. Oracle 创建函数的权限
  6. Codeforces 864E - Fire(dp)
  7. Spring集成–强大的拆分器聚合器
  8. Mysql中的转义字符
  9. dhcp地址分配信息是什么_网络资讯:DHCP 是什么意思
  10. mybatis~动态SQL(1)
  11. 最近一周MOSS的link
  12. org_chart.js 使用方法
  13. 影响科学圈的那些计算机代码
  14. MTK手机充电原理分析及问题总结
  15. 区块链入门系列之P2P
  16. 企业微信(H5打开)调用微信小程序
  17. 直播APP制作时即时聊天功能实现
  18. 3374——数据结构实验之查找二:平衡二叉树
  19. iOS开发:利用SDWebImage实现图片加载与缓存
  20. 软件测试响应时间原则,性能测试二八原则,响应时间2/5/8原则

热门文章

  1. Ubuntu runlevel修改
  2. 微软.NET程序员必上的网站
  3. 提前还贷的python计算程序
  4. 获得系统异常的详细信息
  5. 【python】Python的基本数据类型之数字类型与字符串类型
  6. 【Scala】Scala语言的介绍以及循环的定义(while,for,break,九九乘法表的计算代码)
  7. 后端比android简单,android开发怎么少的了后端(下)
  8. 吃鸡11月15服务器维护,绝地求生11月20日维护到几点 11.20吃鸡更新维护公告
  9. 反射 数据类型_Java基础:反射机制详解
  10. futuretask java 并发请求_Java并发机制(9)--Callable、Future、FutureTask的使用