python简单使⽤xpath查⽹页元素xPath
⼀种HTML和XML的查询语⾔,他能在XML和HTML的树状结构中寻节点
安装
pip install lxml
HTML
超⽂本标记语⾔,是⼀种规范,⼀种标注,是构成⽹页⽂档的主要语⾔
URL
统⼀资源定位器,互联⽹上的每个⽂件都有⼀个唯⼀的URL,它包含的信息指出⽂件的位置
以及浏览应该怎么处理它。
xPath的使⽤
获取⽂本
/
/标签1[@属性1="属性值1"]/标签2[@属性2="属性值"]/..../text()
获取属性值
//标签1[@属性1="属性值1"]/标签2[@属性2="属性值"]/..../@属性n
所需要的html⽂档
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>注册页⾯</title>
<style>
table{
border: 1px solid beige;
}
span{
color: #66CCFF;
}
th{
background-color: gainsboro;
}
tr{
border: 1px solid beige;
}
写网页用什么语言
td{
border: 1px solid aliceblue;
}
.redText{
color: red;
}
.redStar{
color: red;
}
</style>
</head>
<body>
<p>
开始
</p>
<!-- 8列-->
<h3>标题,我是⼀个⼤帅哥</h3>
<ul>
<li>内容1</li>
<li>内容2</li>
<li class="important">内容3important</li>
<li>内容4</li>
<li>内容5</li>
</ul>
<p>
中间
</p>
<div id = "container">
段落⽂字
<a href="www.baidu" title="超链接">跳转到百度⾸页</a>
</div>
<p>
最后
</p>
<form action="">
<table align="center" cellspacing="0">
<tr>
<th colspan="8" align="left">1.会员登录名和密码</th>
</tr>
<tr>
<td>⽤户名:</td>
<td colspan="2"><input type="text"/><span class="redStar">* </span></td>
<td colspan="5"><input type="button" value="检测⽤户名"/><span>5-15位,请使⽤英⽂(a-z、A-Z)、数字(0-9)</span></td>
</tr>
<tr>
<td>密&emsp;码:</td>
<td colspan="2"><input type="password"/><span class="redStar">* </span></td>
<td colspan="5"><span>5-15位,请使⽤英⽂(a-z)、数字(0-9)注意区分⼤⼩写;<br/>密码不能与登录名相同;易记;难猜;</span></td> </tr>
<tr>
<td>再次输⼊密码:</td>
<td colspan="2"><input type="password"/><span class="redStar">* </span></td>
<td colspan="5"><span>两次输⼊的密码必须⼀致</span></td>
</tr>
<tr>
<th colspan="8" align="left">2.姓名和联系⽅式</th>
</tr>
<tr>
<td>真实姓名:</td>
<td colspan="2"><input type="text"/><span class="redStar">* </span></td>
<td colspan="5"><input type="radio" checked="checked" name="Sex" value="male"/>先⽣<input type="radio" name="Sex" value="female"/>⼩</td>
</tr>
<tr>
<td>电⼦邮箱:</td>
<td colspan="2"><input type="text"/><span class="redStar">* </span></td>
<td colspan="5"><span class="redText">⾮常重要!</span><br/><span>这是客户与您联系的⾸选⽅式,请⼀定填写真实。</span></td>
</tr>
<tr>
<td>固定电话:</td>
<td colspan="2"><input type="text"/><span class="redStar">* </span></td>
<td colspan="5"><span>区号+电话号码</span></td>
</tr>
<tr>
<td>公司所在地:</td>
<td colspan="7">
<select>
<option>北京</option>
<option>上海</option>
<option>成都</option>
</select>
<select>
<option>东城
</option>
<option>武侯区</option>
<option>⾦⽜区</option>
</select>
</td>
</tr>
<tr>
<td>街道地址:</td>
<td colspan="5"><input type="text"/><span class="redStar">* </span></td>
<td colspan="2"><span>填写县(区)、街道、门牌号</span></td>
</tr>
<tr>
<td>传真号码:</td>
<td colspan="7"><input type="text"/></td>
</tr>
<tr>
<td>⼿机号码:</td>
<td colspan="7"><input type="text"/></td>
</tr>
<tr>
<td>:</td>
<td colspan="7"><input type="text"/></td>
</tr>
<tr>
<th colspan="8" align="left">3.公司名称和主营业务</th>
</tr>
<tr>
<td>贵公司名称:</td>
<td colspan="2"><input type="text"/><span class="redStar">* </span></td>
<td colspan="5"><span>请填写在⼯商注册的公司/商号全称;<br/>⽆商号的个体经营者填写执照上的姓名,如:张三(个体经营)</span></td>
</tr>
<tr>
<td>你的职位:</td>
<td colspan="2"><input type="text"/><span class="redStar">* </span></td>
<td colspan="5"></td>
</tr>
<tr>
<td>主营⾏业:</td>
<td colspan="2">
<select>
<option>电⼦电⼯</option>
<option>IT科技</option>
<option>⼩猪佩奇</option>
</select>
</td>
<td colspan="5"><span>请正确选择。您会收到该⾏业、该产品的供求信息</span></td>
</tr>
<tr>
<td>主营产品/服务:</td>
<td colspan="7"><input  type="text"/><span class="redStar">* </span><span>3个主要相关品名/服务名,最少要填⼀个。例如:太阳帽,布料,拉链</span> </td>
</tr>
<tr>
<td>:</td>
<td colspan="7"><input  type="text" value=""/></td>
</tr>
<tr>
<td></td>
<td colspan="7"><input type="submit" value="确认提交"/></td>
</tr>
</table>
</form>
</body>
</html>
---------------------
作者:郑清
来源:CSDN
原⽂:blog.csdn/qq_38225558/article/details/82700939
版权声明:本⽂为博主原创⽂章,转载请附上博⽂链接!
所涉及的python代码
from lxml import html
def parse():
""""将html⽂件中的内容,使⽤⼩path进⾏提取"""
#读取⽂件中的内容
f = open('./venv/static_/index.html','r',encodin
g = 'utf-8')
s = f.read()
selector = html.fromstring(s)
#j解析标题
h3 = selector.xpath('/html/body/h3/text()')
print(h3[0])#这⾥取到的是个list,我⽤使⽤列表获取
f.close()
#解析ul⾥⾯的内容
ul = selector.xpath('/html/body/ul/li')
# ul = selector.xpath('//ul/li')也可以使⽤
print(len(ul))
for li  in ul:
print(li.xpath('text()')[0])
#解析tr⾥⾯的内容
# tr = selector.xpath('/html/body/form/table/tr/td/text()')
# print(tr)
#解析ul指定的元素值
ul2 = selector.xpath('/html/body/ul/li[@class="important"]/text()')
print(ul2)
#解析ul指定的元素属性
a = selector.xpath('//div[@id="container"]/a/text()')
print(a[0])
#href属性
alink = selector.xpath('//div[@id="container"]/a/@href')
print(alink[0])
#解析p标签
p = selector.xpath('/html/body/p/text()')
# p = selector.xpath('/html/body/p[last()]/text()') #获取最后⼀个
print(len(p))
print(p[0])
#使⽤浏览的xpath⽣成⼯具
test = selector.xpath('/html/body/form/table/tr[1]/th/text()')#只能借鉴/html/body/form/table/tbody/tr[1]/th print(test[0])
if__name__== '__main__':
parse()