python进阶(lxml的用法)

                   本节处理的文件如下,文件名为:webhtml.html

<!DOCTYPE html>
<html>
<head>
	<title>漏斗图</title>
	<script type="text/javascript" src="./echarts.js"></script>
</head>
<body>
	<div id="main" style="width: 800px;height: 600px">1111</div>
	<article id="main2" style="width: 800px;height: 600px">
		<span>
			logo
			<a href="http://www.baidu.com" style="font-size:15px;">taobao</a>
			<b>hahaha<em>3333</em></b>
			<a href="www.baidu.com">taobao2</a>
		</span>
	</article>
	<div id="last">last... ...</div>
	<div class="one">11111111111111111111111</div>
	<div class="one two" name="sec" data-foo="value">22222222222222222222222</div>
	<div id="left">
		<a href="http://www.taobao1.com">11111</a>
		<a href="http://www.taobao2.com">333333</a>
		<a href="http://www.taobao3.com">4444</a>
		<a href="http://www.taobao4.com">55555</a>
	</div>
	<script type="text/javascript">
		var myChart=echarts.init(document.getElementById('main'))
		var option={
			title:{
				text:"你的附近哪家自助货架比较多",
				subtext:"数据地区:上海",
			},
			tooltip:{
				// trigger:'item'   //not axis
			},
			legend:{
				orient:"vertical",
				left:"left",
				top:"center",
				data:['猩便利','小u货架','友宝','峰小柜','小e微店']
				//data中的名字和series 中data中的name相等
			},
			toolbox:{
				// show:true,
				feature:{              //feature  不是 true
					// mark:{
					// 	show:true
					// },
					dataView:{
						show:true,
						readOnly:true
					},
					restore:{
						show:true
					},
					saveAsImage:{
						show:true
					}
				}
			},
			series:[{
				name:"货架详情",
				type:"funnel",
				left:"30%",
				max:100,
				min:0,
				data:[
					{
						value:100,
						name:"猩便利",
					},{
						value:80,
						name:"友宝"
					},{
						value:60,
						name:"峰小柜"
					},{
						name:"小u货架",
						value:20
					},{
						name:"小e微店",
						value:40
					}
				]
			}]
		}
		myChart.setOption(option)
 
	</script>
</body>
</html>

一、lxml的基本知识:

           ①xpath路径可以放在浏览器中查看。

           ②string得到结果是str,/text()得到的结果是list

           ③  /@属性名  得到的结果也是list

1、lxml对象的创建:

(1)通过resquests响应内容:

from lxml import etree
import requests
                              响应内容
responce1 = requests.get('https://www.baidu.com').content.decode('utf-8')
html_lxml = etree.HTML(responce1)    创建lxml对象          

(2)打开本地文件: 

2、将lxml对象序列化:

result = etree.tostring(html_lxml,pretty_print=True,encoding='utf-8').decode('utf-8')
print(result)

二、xpath语法:

1、选取节点:

2、谓语:

3、xpath通配符:

4、实例:

扫描二维码关注公众号,回复: 4135307 查看本文章

5、xpath运算符:

                      其中 或(|)比较常用。

 < >= 等运算符用于标签内容比较,如例:

 6、xpath获得标签属性和标签内容:

                获得是内容,而不是标签本身。

  • ① /text() 获取第一层节点的所有内容,不包括子节点,且结果是list。
  • ② /@属性名: 获得标签的属性,结果也是 list。
  • ③ string 获得所有节点的内容,包括子节点,结果是 str 。

7、实例:

猜你喜欢

转载自blog.csdn.net/qq_16555103/article/details/84198546