码迷,mamicode.com
首页 > 编程语言 > 详细

《Python网络数据采集》读书笔记(二)

时间:2018-03-30 00:14:07      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:find   child   descendant   sibling   parent   

1、通过的名称和属性查找标签

和之前一样,抓取整个页面,然后创建一个BeautifulSoup对象。这里面“lxml”解析器需要另外下载。

pip3 install lxml
>>> from urllib.request import urlopen
>>> from bs4 import BeautifulSoup
>>> html = urlopen("http://www.pythonscraping.com/pages/warandpeace.html")
>>> bsObj = BeautifulSoup(html, "lxml")


finAll()可以获取页面中所有指定的标签,抽取只包含在<span class="green"></span>标签里的文字,这样就会得到一个人物名称的列表。.get_text()会把所有的标签都清除,返回一个只包含文字的字符串。

>>> nameList = bsObj.findAll("span", {"class":"green"})
>>> for name in nameList:
        print(name.get_text())



2、详解finAll()和find()的参数

findAll(tag, attributes, recursive, text, limit, keywords)

find(tag, attributes, recursive, text, keywords)


tag--可以传一个标签的名称或多个标签名称组成的列表做参数;如下示例将返回一个包含HTML文档中所有标题标签的列表

>>> bsObj.findAll({"h1", "h2", "h3", "h4", "h5", "h6"})
[<h1>War and Peace</h1>, <h2>Chapter 1</h2>]


attributes--字典封装一个标签的若干属性和对应的属性值;如下示例会返回HTML文档里红色与绿色两种颜色的span标签

>>> bsObj.findAll("span", {"class":{"green", "red"}})


recursive--布尔变量(默认为True)查找标签参数的所有子标签,及子标签的子标签;为False时,只查找文档的一级标签


text--是用标签的文本内容去匹配;如下示例作用是查找网页中包含“the prince”内容的标签数量

>>> nameList=bsObj.findAll(text="the prince")
>>> print(len(nameList))
7


limit--按照网页上的顺序排序,获取的前 x 项结果(等于1时等价于find)


keywords--可以让你选择那些具有指定属性的标签

>>> allText = bsObj.findAll(id="text")
>>> print(allText[0].get_text())

下面两行代码是完全一样的

bsObj.findAll(id="text")
bsObj.findAll("", {"id":"text"})

由于class为关键字,使用keywords指定时需要多加一个下划线

bsObj.findAll(class_="green")
bsObj.findAll("", {"class":"green"})


3、BeautifulSoup对象

BeautifulSoup对象:如前面代码示例中的bsObj

Tag对象:BeautifulSoup对象通过find和findAll或直接调用子标签获取的一列象或单个对象(如bsObj.div.h1)

NavigableString对象:表示标签里的文字

Comment对象:查找 HTML 文档的注释标签


4、标签解析树的导航:通过标签在文档中的位置来查找标签。


处理子标签与后代标签:

子标签(child)是一个父标签的下一级。

后代标签(descendant)是指一个父标签下面所有级别的标签。

同样的

>>> from urllib.request import urlopen
>>> from bs4 import BeautifulSoup
>>> html = urlopen("http://www.pythonscraping.com/pages/page3.html")
>>> bsObj = BeautifulSoup(html, "lxml")

只想找出子标签,可以用.children;如下示例会打印giftList表格中所有产品的数据行(如使用.descendants将打印出二十几个标签)

>>> for child in bsObj.find("table", {"id":"giftList"}).children:
print(child)


处理兄弟标签:

next_siblings和previous_siblings将返回一组标签

next_sibling和previous_sibling将返回单个标签

如下示例会打印产品列表里除第一行外的所有行的产品

>>> for sibling in bsObj.find("table", {"id":"giftList"}).tr.next_siblings:
        print(sibling)


处理父标签:

parent 和 parents

如下示例会打印出指定图片对应的商品价格

>>> print(bsObj.find("img", {"src":"../img/gifts/img1.jpg"}).parent.previous_sibling.get_text())
$15.00


《Python网络数据采集》读书笔记(二)

标签:find   child   descendant   sibling   parent   

原文地址:http://blog.51cto.com/9473774/2092737

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!