Task 02:爬虫2

爬虫二

Beautiful Soup库入门

  1. 学习beautifulsoup基础知识。

  2. 使用beautifulsoup解析HTML页面。

    • Beautiful Soup 是一个HTML/XML 的解析器,主要用于解析和提取 HTML/XML 数据。
    • 它基于HTML DOM 的,会载入整个文档,解析整个DOM树,因此时间和内存开销都会大很多,所以性能要低于lxml。
    • BeautifulSoup 用来解析 HTML 比较简单,API非常人性化,支持CSS选择器、Python标准库中的HTML解析器,也支持 lxml 的 XML解析器。
    • 虽然说BeautifulSoup4 简单容易比较上手,但是匹配效率还是远远不如正则以及xpath的,一般不推荐使用,推荐正则的使用。
  • 第一步:pip install beautifulsoup4 ,万事开头难,先安装 beautifulsoup4,安装成功后就完成了第一步。

  • 第二步:导入from bs4 import BeautifulSoup

  • 第三步:创建 Beautiful Soup对象 soup = BeautifulSoup(html,‘html.parser’)

1.1 Beautiful Soup库的基本元素

  1. Beautiful Soup库的理解:
    Beautiful Soup库是解析、遍历、维护“标签树”的功能库,对应一个HTML/XML文档的全部内容

  2. BeautifulSoup类的基本元素:

    • Tag 标签,最基本的信息组织单元,分别用<>和</>标明开头和结尾;
    • Name 标签的名字,<p>…</p>的名字是'p',格式:<tag>.name;
    • Attributes 标签的属性,字典形式组织,格式:<tag>.attrs;
    • NavigableString 标签内非属性字符串,<>…</>中字符串,格式:<tag>.string;
    • Comment 标签内字符串的注释部分,一种特殊的Comment类型;
# 导入bs4库
from bs4 import BeautifulSoup
import requests # 抓取页面

r = requests.get('https://python123.io/ws/demo.html') # Demo网址
demo = r.text  # 抓取的数据
demo
'<html><head><title>This is a python demo page</title></head>\r\n<body>\r\n<p class="title"><b>The demo python introduces several python courses.</b></p>\r\n<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>\r\n</body></html>'
# 解析HTML页面
soup = BeautifulSoup(demo, 'html.parser')  # 抓取的页面数据;bs4的解析器
# 有层次感的输出解析后的HTML页面
print(soup.prettify())
'<html>\n <head>\n  <title>\n   This is a python demo page\n  </title>\n </head>\n <body>\n  <p class="title">\n   <b>\n    The demo python introduces several python courses.\n   </b>\n  </p>\n  <p class="course">\n   Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\n   <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">\n    Basic Python\n   </a>\n   and\n   <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">\n    Advanced Python\n   </a>\n   .\n  </p>\n </body>\n</html>'
1)标签,用soup.<tag>访问获得:
  • 当HTML文档中存在多个相同<tag>对应内容时,soup.<tag>返回第一个
soup.a # 访问标签a
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
soup.title
<title>This is a python demo page</title>
2)标签的名字:每个<tag>都有自己的名字,通过soup.<tag>.name获取,字符串类型
soup.a.name
'a'
soup.a.parent.name
'p'
soup.p.parent.name
'body'
3)标签的属性,一个<tag>可以有0或多个属性,字典类型,soup.<tag>.attrs
tag = soup.a
print(tag.attrs)
print(tag.attrs['class'])
print(type(tag.attrs))
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
['py1']
<class 'dict'>
4)Attributes:标签内非属性字符串,格式:soup.<tag>.string, NavigableString可以跨越多个层次
print(soup.a.string)
print(type(soup.a.string))
Basic Python
<class 'bs4.element.NavigableString'>
5)NavigableString:标签内字符串的注释部分,Comment是一种特殊类型(有-->)
print(type(soup.p.string))
<class 'bs4.element.NavigableString'>
6) .prettify()为HTML文本<>及其内容增加更加’\n’,有层次感的输出
.prettify()可用于标签,方法:<tag>.prettify()
print(soup.prettify())
<html>
 <head>
  <title>
   This is a python demo page
  </title>
 </head>
 <body>
  <p class="title">
   <b>
    The demo python introduces several python courses.
   </b>
  </p>
  <p class="course">
   Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
   <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
    Basic Python
   </a>
   and
   <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">
    Advanced Python
   </a>
   .
  </p>
 </body>
</html>
print(soup.a.prettify())
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
 Basic Python
</a>
7)bs4库将任何HTML输入都变成utf‐8编码
Python 3.x默认支持编码是utf‐8,解析无障碍
newsoup = BeautifulSoup('<a>中文</a>', 'html.parser')
print(newsoup.prettify())
<a>
 中文
</a>

1.2 基于bs4库的HTML内容遍历方法

HTML基本格式:<>…</>构成了所属关系,形成了标签的树形结构

  • 标签树的下行遍历
    • .contents 子节点的列表,将<tag>所有儿子节点存入列表
    • .children 子节点的迭代类型,与.contents类似,用于循环遍历儿子节点
    • .descendants 子孙节点的迭代类型,包含所有子孙节点,用于循环遍历
  • 标签树的上行遍
    • .parent 节点的父亲标签
    • .parents 节点先辈标签的迭代类型,用于循环遍历先辈节点
  • 标签树的平行遍历
    • .next_sibling 返回按照HTML文本顺序的下一个平行节点标签
    • .previous_sibling 返回按照HTML文本顺序的上一个平行节点标签
    • .next_siblings 迭代类型,返回按照HTML文本顺序的后续所有平行节点标签
    • .previous_siblings 迭代类型,返回按照HTML文本顺序的前续所有平行节点标签
  • 详见:https://www.cnblogs.com/mengxiaoleng/p/11585754.html#_label0
标签树的下行遍历
import requests
from bs4 import BeautifulSoup

r=requests.get('http://python123.io/ws/demo.html')
demo=r.text
soup=BeautifulSoup(demo,'html.parser')
print(soup.contents)# 获取整个标签树的儿子节点
[<html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>
</body></html>]
print(soup.body.content)#返回标签树的body标签下的节点
None
print(soup.head)#返回head标签
<head><title>This is a python demo page</title></head>
for child in soup.body.children:#遍历儿子节点
    print(child)
<p class="title"><b>The demo python introduces several python courses.</b></p>


<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>
for child in soup.body.descendants:#遍历子孙节点
    print(child)
<p class="title"><b>The demo python introduces several python courses.</b></p>
<b>The demo python introduces several python courses.</b>
The demo python introduces several python courses.


<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>
Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:

<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
Basic Python
 and 
<a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>
Advanced Python
.
标签树的上行遍历
soup.title.parent
<head><title>This is a python demo page</title></head>
soup.title.parent
<head><title>This is a python demo page</title></head>
soup.parent
for parent in soup.a.parents: # 遍历先辈的信息
    if parent is None:
        print(parent)
    else:
        print(parent.name)
p
body
html
[document]
标签树的平行遍历

注意:

  • 标签树的平行遍历是有条件的
  • 平行遍历发生在同一个父亲节点的各节点之间
  • 标签中的内容也构成了节点
print(soup.a.next_sibling)#a标签的下一个标签
 and 
print(soup.a.next_sibling.next_sibling)#a标签的下一个标签的下一个标签
<a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>
print(soup.a.previous_sibling)#a标签的前一个标签
Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
print(soup.a.previous_sibling.previous_sibling)#a标签的前一个标签的前一个标签
None
for sibling in soup.a.next_siblings:#遍历后续节点
    print(sibling)
 and 
<a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>
.
for sibling in soup.a.previous_sibling:#遍历之前的节点
    print(sibling)

1.3 基于bs4库的HTML内容的查找方法

  • <>.find_all(name, attrs, recursive, string, **kwargs)
    • 参数:
    • ∙ name : 对标签名称的检索字符串
    • ∙ attrs: 对标签属性值的检索字符串,可标注属性检索
    • ∙ recursive: 是否对子孙全部检索,默认True
    • ∙ string: <>…</>中字符串区域的检索字符串
      • 简写:
      • <tag>(…) 等价于 <tag>.find_all(…)
      • soup(…) 等价于 soup.find_all(…)
  • 扩展方法:
    • <>.find() 搜索且只返回一个结果,同.find_all()参数
    • <>.find_parents() 在先辈节点中搜索,返回列表类型,同.find_all()参数
    • <>.find_parent() 在先辈节点中返回一个结果,同.find()参数
    • <>.find_next_siblings() 在后续平行节点中搜索,返回列表类型,同.find_all()参数
    • <>.find_next_sibling() 在后续平行节点中返回一个结果,同.find()参数
    • <>.find_previous_siblings() 在前序平行节点中搜索,返回列表类型,同.find_all()参数
    • <>.find_previous_sibling() 在前序平行节点中返回一个结果,同.find()参数
import requests
from bs4 import BeautifulSoup

r = requests.get('http://python123.io/ws/demo.html')
demo = r.text
soup = BeautifulSoup(demo,'html.parser')
soup
<html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>
</body></html>
# name : 对标签名称的检索字符串
soup.find_all('a')
[<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>,
 <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]
soup.find_all(['a', 'p'])
[<p class="title"><b>The demo python introduces several python courses.</b></p>,
 <p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
 <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>,
 <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>,
 <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>]
# attrs: 对标签属性值的检索字符串,可标注属性检索
soup.find_all("p","course")
[<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
 <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>]
soup.find_all(id="link") # 完全匹配才能匹配到
[]
#  recursive: 是否对子孙全部检索,默认True
soup.find_all('p',recursive=False)
[]
# string: <>…</>中字符串区域的检索字符串
soup.find_all(string = "Basic Python") # 完全匹配才能匹配到
[]

1.4 实战:中国大学排名定向爬取

  • 爬取url:http://www.zuihaodaxue.cn/zuihaodaxuepaiming2019.html
  • 爬取思路:
    1. 从网络上获取大学排名网页内容
    2. 提取网页内容中信息到合适的数据结构(二维数组)-排名,学校名称,总分
    3. 利用数据结构展示并输出结果
# 导入库
import requests
from bs4 import BeautifulSoup
import bs4
1. 从网络上获取大学排名网页内容
def getHTMLText(url):
    try:
        r = requests.get(url, timeout=30) 
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return ""
2. 提取网页内容中信息到合适的数据结构(二维数组)
  1. 查看网页源代码,观察并定位到需要爬取内容的标签;
  2. 使用bs4的查找方法提取所需信息-‘排名,学校名称,总分’
def fillUnivList(ulist, html):
    soup = BeautifulSoup(html, "html.parser")
    for tr in soup.find('tbody').children: 
        if isinstance(tr, bs4.element.Tag):
            tds = tr('td')
            # 根据实际提取需要的内容,
            ulist.append([tds[0].string, tds[1].string, tds[3].string])  
3. 利用数据结构展示并输出结果
# 对中英文混排输出问题进行优化:对format(),设定宽度和添加参数chr(12288)
def printUnivList(ulist, num=20):
    tplt = "{0:^10}\t{1:{3}^10}\t{2:^10}"
    print(tplt.format('排名', '学校名称', '总分', chr(12288)))
    for i in range(num):
        u = ulist[i]
        print(tplt.format(u[0], u[1], u[2], chr(12288)))
u_info = [] # 存储爬取结果的容器
url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2019.html'
html = getHTMLText(url)
html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值