from urllib import request
from lxml import etree
# url = '''http://bangumi.tv/anime/browser?sort=rank'''
# response = request.urlopen(url)
# html = response.read()
html = '''
id="item_1728" class="item even clearit">
<a href="/subject/1728" class="subjectCover cover ll">
<span class="image">
<img src="//lain.bgm.tv/pic/cover/s/71/37/1728_HLsCr.jpg" class="cover">
</span>
<span class="overlay"></span>
</a>
<div class="inner">
<h3> <a href="/subject/1728" class="l">浪客剑心 追忆篇</a> <small class="grey">るろうに剣心 -明治剣客浪漫譚- 追憶編</small>
</h3> <span class="rank"><small>Rank </small>12</span>
<p class="info tip">
4话 / 1999年2月20日 </p>
<p class="rateInfo">
<span class="sstars9 starsinfo"></span> <small class="fade">8.8</small> <span class="tip_j">(2165人评分)</span>
</p> </div>
</li>
''' html = etree.HTML(html)
result = etree.tostring(html)
print(result)
li_all = html.xpath('//a')
print(li_all)#[<Element a at 0x2ebe198>, <Element a at 0x2ebe170>]
# li_all = html.xpath('//a/@href')['/subject/1728', '/subject/1728']
# print(li_all)
li_all = html.xpath('//a/@class')#['subjectCover cover ll', 'l']
print(li_all)
li_all = html.xpath('//a[@href="/subject/1728"]')#获取所有href等于这个的标签
print(li_all)
li_all = html.xpath('//div/a')#获取a标签下所有的子span标签
print(li_all)
li_all = html.xpath('//div//a')#获取a标签下所有的子孙span标签
print(li_all)
li_all = html.xpath('//div//a//@class')#获取a标签下所有的子孙span标签
print(li_all)
li_all = html.xpath('//div//p[last()]/span')#获取最后一个p元素的所有span标签
print(li_all)
li_all = html.xpath('//div//p[last()-1]')#获取倒数第二个个p元素的所有span标签
print(li_all[0].text)
li_all = html.xpath('string()')#过滤标签,返回所有文本
print(li_all)
li_all = html.xpath('//text()')#过滤标签,将每个文本存放于列表中
print(li_all)
li_all = html.xpath('//text()')
print(li_all[0].getparent().tag)#根据文本返回它的标签名
print(li_all[1].is_tail)
print(li_all[1].is_tail)#判断是普通文本还是tail文本

lxml的使用的更多相关文章

  1. requests的content与text导致lxml的解析问题

    title: requests的content与text导致lxml的解析问题 date: 2015-04-29 22:49:31 categories: 经验 tags: [Python,lxml, ...

  2. python3安装lxml(windows)

    爬虫时通常要安装LXML,对于通过一下命令行 1 pip install lxml 出现如下错误的解决方法 1 lxml Unable to find vcvarsall.bat 1. 安装wheel ...

  3. Python爬虫利器三之Xpath语法与lxml库的用法

    前面我们介绍了 BeautifulSoup 的用法,这个已经是非常强大的库了,不过还有一些比较流行的解析库,例如 lxml,使用的是 Xpath 语法,同样是效率比较高的解析方法.如果大家对 Beau ...

  4. 使用python+xpath 获取https://pypi.python.org/pypi/lxml/2.3/的下载链接

    使用python+xpath 获取https://pypi.python.org/pypi/lxml/2.3/的下载链接: 使用requests获取html后,分析html中的标签发现所需要的链接在& ...

  5. python笔记:windows 下安装 python lxml

    原文:http://blog.csdn.net/zhaokuo719/article/details/8209496 windows 环境下安装 lxml python 1.首先保证你的python ...

  6. python lxml install

    之前记得安装libxslt和libxml yum install libxml* -yyum install libxslt* -y wget http://lxml.de/files/lxml-3. ...

  7. windows下使用pip安装python的第三方lxml库

    lxml是Python语言里和XML以及HTML工作的功能最丰富和最容易使用的库.lxml库的安装和python其他第三方库的安装方法是一样的,只是可能由于一些细节上的失误导致安装失败. 工具 Pyt ...

  8. Python: Win7下使用 pip install lxml 无法安装lxml?

    1.在网址 http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml 下,搜索lxml,下载Python对应的lxml版本.如下图: 2.打开cmd,进入到lxm ...

  9. 【python】安装python第三方库lxml时,遇到问题:[ERROR: 'xslt-config' 不是内部或外部命令,也不是可运行的程序]

    一.概述 lxml介绍http://lxml.de/ 二.问题 ERROR: 'xslt-config' 不是内部或外部命令,也不是可运行的程序 三.解决方法 Scrapy在Windows上的安装笔记 ...

  10. Windows下安装Python lxml库(无废话版)

    python官网:python-2.7.12.amd64.msihttps://pypi.python.org/pypi/setuptools:setuptools-28.6.0.zipsetupto ...

随机推荐

  1. selenium 操作复选框

    场景 从上一节的例子中可以看出,webdriver可以很方便的使用findElement方法来定位某个特定的对象,不过有时候我们却需要定位一组对象, 这时候就需要使用findElements方法. 定 ...

  2. 饮冰三年-人工智能-Python-11之HelloWorld

    1:安装不在介绍,下载软件下一步即可,配置环境变量(Python37会默认配置环境变量的) D:\Programs\Python\Python37\Scripts\;D:\Programs\Pytho ...

  3. Python作业之分页显示内容

    #coding:utf8 user_list =[] for i in range(1,302): tmp = "{'user':'alex-%s,'email':'alex%s@email ...

  4. Windows系统下安装运行Kafka

    一.安装JAVA JDK 1.下载安装包 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151. ...

  5. How to disable Microsoft Compatibility Telemetry

    Issue: How to disable Microsoft Compatibility Telemetry (CompatTelRunner.exe)?   Option : Disable Mi ...

  6. 分布式系统的BASE理论

    一.BASE理论 eBay的架构师Dan Pritchett源于对大规模分布式系统的实践总结,在ACM上发表文章提出BASE理论,BASE理论是对CAP理论的延伸,核心思想是即使无法做到强一致性(St ...

  7. 未在本地计算机上注册“OraOLEDB.Oracle.1”提供程序。

     问题描述:运行访问oracle数据库的.net程序时,弹出错误"未在本地计算机上注册“OraOLEDB.Oracle.1”提供程序". 系统环境:windows server 2 ...

  8. error: Microsoft Visual C++ 14.0 is required.

    缺少包的依赖!! 解决办法1. 安装 Microsoft visual c++ 14.0 https://964279924.ctfile.com/fs/1445568-239446865 或 htt ...

  9. Codeforces 844F Anti-Palindromize 最小费用流

    Anti-Palindromize 想到网络流就差不多了, 拆拆点, 建建边. #include<bits/stdc++.h> #define LL long long #define f ...

  10. LoadRunner的函数

     一.基础函数 在VU左边导航栏中,有三个LR框架函数,分别是vuser_init(),Action(),vuser_end(). 这三个函数存在于任何Vuser类型的脚本中: ●vuser_init ...