标签:a标签 实验 使用方法 image com 导入 文章 解析 基本
写在前面的话 :上一篇文章我们利用requests进行了一些爬虫小实验,但是想要更顺利的深入爬虫学习,了解一些解析网页的方法肯定是必须的,所以接下来我们就一起来学习lxml.etree模块的基础使用方法吧
温馨提示 :博主使用的系统为win10,使用的python版本为3.6.5
若想了解xpath,我们首先需要知道什么是xml文档,其实简单地说,xml文档就是由一系列节点构成的树,例如
<html>
    <body>
        <div>
            <p>Hello world<p>
            <a href="/home">Click here</a>
        </div>
    </body>
</html>xml文档常见的节点有
xml文档常见的节点间关系有
而xpath则是一种用来确定xml文档中某部分位置的语言,它的全称是XML路径语言(XML Path Language),对于网页解析来说,xpath比正则表达式来得更方便更简洁,故python中专门提供了一个特殊的模块——lxml库中的etree模块用于处理xpath,我们可以使用以下命令进行安装
$ pip install lxml>>> from lxml import etree在这里为了简便起见,我们自己构造一个简单的xml文档
>>> sc = ‘‘‘
<html>
    <head>
        <meta charset=UTF-8>
        <link rel=stylesheet href=style/base.css>
        <link rel=stylesheet href=style/home.css>
        <base href="https://www.example.com"/>
        <title>Example website</title>
    <body>
        <div id="images" class="content">
            <a href="image1.html">Image1<br/><img src="image1.jpg"/>
            <a href="image2.html">Image2<br/><img src="image2.jpg"/>
            <a href="image3.html">Image3<br/><img src="image3.jpg"/>
            <a href="image4.html">Image4<br/><img src="image4.jpg"/>
            <a href="image5.html">Image5<br/><img src="image5.jpg"/>
‘‘‘#可以使用HTML()方法构造_Element对象并自动补全不完整代码
>>> html = etree.HTML(sc)
#构造对象结果检查
>>> type(html)
<class ‘lxml.etree._Element‘>
#补全代码结果检查,注意tostring()方法用于将_Element对象转化成bytes类型字符串,decode(‘utf-8‘)方法用于将bytes类型字符串转化为str类型字符串
>>> print(etree.tostring(html).decode(‘utf-8‘))
<html>
    <head>
        <meta charset="UTF-8"/>
        <link rel="stylesheet" href="style/base.css"/>
        <link rel="stylesheet" href="style/home.css"/>
        <base href="https://www.example.com"/>
        <title>Example website</title>
    </head>
    <body>
        <div id="images" class="content">
            <a href="image1.html">Image1<br/><img src="image1.jpg"/></a>
            <a href="image2.html">Image2<br/><img src="image2.jpg"/></a>
            <a href="image3.html">Image3<br/><img src="image3.jpg"/></a>
            <a href="image5.html">Image5<br/><img src="image5.jpg"/></a>
        </div>
    </body>
</html>可以使用xpath()方法进行匹配,注意该方法返回匹配列表,且列表中的每一项都是_Element对象
(1)/ 表示子代,例如E1/E2表示E1子节点中的E2节点,/E表示文档子节点中的E节点
>>> test = html.xpath(‘/html/body/div/a‘)
>>> print(test)
[<Element a at 0x3843bc0>, <Element a at 0x3843c10>, <Element a at 0x3843c38>, <Element a at 0x3843c60>, <Element a at 0x3843c88>](2)// 表示后代,例如E1//E2表示E1后代节点中的E2节点,//E表示文档后代节点中的E节点
>>> test = html.xpath(‘//a‘)
>>> print(test)
[<Element a at 0x3843bc0>, <Element a at 0x3843c10>, <Element a at 0x3843c38>, <Element a at 0x3843c60>, <Element a at 0x3843c88>](3)* 表示属性节点,例如E/*表示E子节点中的所有节点
>>> test = html.xpath(‘/html/*‘)
>>> print(test)
[<Element head at 0x3843be8>, <Element body at 0x3843c10>](4)text() 表示文本节点,例如E/text()表示E子节点中的文本节点
>>> test = html.xpath(‘/html/head/title/text()‘)
>>> print(test)
[‘Example website‘](5)@ATTR 表示属性节点,例如E/@ATTR表示E子节点中的ATTR属性节点
>>> test = html.xpath(‘//a/@href‘)
>>> print(test)
[‘image1.html‘, ‘image2.html‘, ‘image3.html‘, ‘image4.html‘, ‘image5.html‘](6)谓语 用于匹配指定的标签
#指定第二个a标签
>>> test = html.xpath(‘//a[2]‘)
>>> print(test)
[<Element a at 0x3843c88>]
#指定前两个a标签
>>> test = html.xpath(‘//a[position()<=2]‘)
>>> print(test)
[<Element a at 0x3843c60>, <Element a at 0x3843c88>]
#指定带有href属性的a标签
>>> test = html.xpath(‘//a[@href]‘)
>>> print(test)
[<Element a at 0x3843c38>, <Element a at 0x385c300>, <Element a at 0x385c2d8>, <Element a at 0x385c350>, <Element a at 0x385c328>]
#指定带有href属性且值为image1.html的a标签
>>> test = html.xpath(‘//a[@href="image1.html"]‘)
>>> print(test)
[<Element a at 0x3843c38>]我们先用xpath()方法得到匹配列表tests,tests中的每一项都是一个 _Element对象
>>> tests = html.xpath(‘//a‘)(1)属性tag 返回标签名
>>> for test in tests:
        test.tag
‘a‘
‘a‘
‘a‘
‘a‘
‘a‘(2)属性 attrib 返回属性与值组成的字典
>>> for test in tests:
        test.attrib
{‘href‘: ‘image1.html‘}
{‘href‘: ‘image2.html‘}
{‘href‘: ‘image3.html‘}
{‘href‘: ‘image4.html‘}
{‘href‘: ‘image5.html‘}(3)方法 get() 返回指定属性的值
>>> for test in tests:
        test.get(‘href‘)
‘image1.html‘
‘image2.html‘
‘image3.html‘
‘image4.html‘
‘image5.html‘(4)属性 text 返回文本值
>>> for test in tests:
        test.text
‘Image1‘
‘Image2‘
‘Image3‘
‘Image4‘
‘Image5‘写在后面的话 :现在我们已经学习完requests和lxml.etree模块的基础使用方法了,下一篇文章我们将利用它们进行一个基础的爬虫实战训练,谢谢大家
标签:a标签 实验 使用方法 image com 导入 文章 解析 基本
原文地址:https://www.cnblogs.com/wsmrzx/p/9520942.html