-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"/> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"/> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<ul> | ||
<li id="l1" class="c1">北京</li> | ||
<li id="l2">上海</li> | ||
<li id="c3">深圳</li> | ||
<li id="c4">武汉</li> | ||
</ul> | ||
|
||
<!-- <ul> | ||
<li>大连</li> | ||
<li>西安</li> | ||
<li>沈阳</li> | ||
</ul> --> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# 浏览器中xpath插件快捷键:ctrl+shift+x | ||
|
||
from lxml import etree | ||
|
||
# xpath解析 | ||
# (1)本地文件 etree.parse | ||
# (2)服务器响应的数据 response.read().decode('utf-8') ***** etree.HTML() | ||
|
||
# xpath解析本地文件 | ||
tree = etree.parse('17_解析_xpath的基本使用.html') | ||
|
||
print(tree) | ||
|
||
# tree.xpath('xapth') | ||
|
||
# 查找ul下面的li | ||
# //表示其子孙后代 /表示其直接子节点 | ||
# li_list = tree.xpath('//body/ul/li') | ||
|
||
# 查找所有有id属性的li标签 | ||
# text() 获取标签内容 | ||
# li_list = tree.xpath('//ul/li[@id]/text()') | ||
|
||
# 找到id为l1的li标签 注意引号的问题 | ||
# li_list = tree.xpath('//ul/li[@id="l1"]/text()') | ||
|
||
# 查找到id为l1的li标签的class的属性值 | ||
# li_list = tree.xpath('//ul/li[@id="l1"]/@class') | ||
|
||
# 查询id里面包含l的li标签 模糊查询 | ||
# li_list = tree.xpath('//ul/li[contains(@id,"l")]/text()') | ||
|
||
# 查询id值以c开头的li标签 | ||
# li_list = tree.xpath('//ul/li[starts-with(@id,"c")]/text()') | ||
|
||
# 查询id为l1和class为c1的数据 逻辑与运算 | ||
# li_list = tree.xpath('//ul/li[@id="l1" and @class="c1"]/text()' | ||
|
||
# | ||
li_list = tree.xpath('//ul/li[@id="l1"]/text() | // ul/li[@id="l2"]/text()') | ||
|
||
|
||
# 判断列表的长度 | ||
print(li_list) | ||
print(len(li_list)) |