xpath解析
抓取主页面当中所有壁纸的链接地址
xpath是专门针对xml而创建的表达式语言,可以直接从xml中提取表达式数据;也可以取html取数据;html是xml的子集。
1.按照lxml安装包
在python终端输入 pip install lxml
from lxml import etree
# 或者
# from lxml import html
# etree = html.etree
# 需要加载准备解析的数据
f = open("text.html",mode="r",encoding='utf-8')
pageSource = f.read()
#print(pageSource)
# 加载数据,返回element对象
et = etree.HTML(pageSource)
# print(et) <Element html at 0x1f349424980>
# 从elemnt提取界面所有内容
# xpath的语法
result = et.xpath("/html/body/span/text()") # text()提取标签中的文本信息
print(result)
加载需要解析的html文件,但要提取数据要定位html的<>内容,例如
text.html,"/html/body/span/"定位到<span>我爱你</span》内容,那么要提取里面文本需要加上给text(),即可提取定位位置的文本信息。
<!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8"><title>Title</title></head><body><span>我爱你</span> <ul><li><a href="http://www.baidu.com">百度</a></li><li><a href="http://www.google.com">谷歌</a></li><li><a href="http://www.sohu.com">搜孤</a></li></ul><ol><li><a href="http://feiji">飞机</a></li><li><a href="http://dapao">大炮</a></li><li><a href="http://huoche">火车</a></li></ol><div class="job">浙江</div><div class="common">美女</div></body> </html>
<a href= >XX</a>超链接,XX是展示在页面上的文字,用户可以点击,点击后就跳转到href=的链接地址
需求:提取超链接中的文字(百度,谷歌,搜狐被<ul>包围,飞机,大炮,火车被<ol>包围写两个提取信息代码或者使用通配符*)
(* 通配符,什么都可以,满足后面即可)
result = et.xpath("/html/body/*/li/a/text()")
print(result)
# 输出结果 ['百度', '谷歌', '搜孤', '飞机', '大炮', '火车']
需求:提取a标签中的属性,提取代码中的超链接,使用@href,提取html中的链接地址
@表示属性,@href提取a标签中的href属性
# 提取a标签中的属性,即超链接
result = et.xpath("/html/body/*/li/a/@href") #@表示属性,@href提取a标签中的href属性
print(result)
# 输出结果:['http://www.baidu.com', 'http://www.google.com', 'http://www.sohu.com', 'http://feiji', 'http://dapao', 'http://huoche']
优化:欠缺提取信息的索引太多,可以使用//优化,,//表示任意位置
result = et.xpath("//a/@href") # //表示任意位置
print(result)
# 输出结果:['http://www.baidu.com', 'http://www.google.com', 'http://www.sohu.com', 'http://feiji', 'http://dapao', 'http://huoche']
限定:假如要提取div中的信息,并且只要浙江,不要美女,要对div中class属性进行限定,即可提取自己想要的信息,而不是把符合div的数据全部提取
[@属性=‘’]在属性上限定,不把符合div的数据全部提出
result = et.xpath("//div[@class='job']/text()") # [@属性=‘’]在属性上限定,不把符合div的数据全部提出
print(result)
# 输出:['浙江']
需求:要ul中的信息,并且要求文本和属性一一对应;
思路:对li进行遍历,逐个提取各li中的所有信息 ./表示当前元素
# 带循环的
result = et.xpath("/html/body/ul/li")
for item in result:href = item.xpath("./a/@href")[0] # ./表示当前元素text = item.xpath("./a/text()")[0] # ./表示当前元素print(href,text)
# 输出结果
# http://www.baidu.com 百度
# http://www.google.com 谷歌
# http://www.sohu.com 搜孤
主页面分析


</li><li class="photo-list-padding"><a class="pic" href="[/bizhi/3114_39082_2.html](https://desk.zol.com.cn/bizhi/3114_39082_2.html)" target="_blank" hidefocus="true"><span title="小黄人可爱高清壁纸大全">小黄人可爱高清壁纸大全
找数据,先找a标签,查看a标签中的href数据,点入确定照片
-
拿到页面源代码
-
提取所有a标签中的href数据,要对a标签中的内容进行限定,我们这里要
中的内容,即提取ul>li>a标签中的href的值
问题及解决方法:
1.如果出现乱码,则查看源代码的编码方式,图中为gb2312,即gbk编码,我们把encode改一下就行。

2.如果出现503 Service Unavailable 尝试把请求头信息一致即可完成。
import requests
from lxml import etree
# 提取源代码
url = "https://desk.zol.com.cn/dongman/good_1.html"
head = {
"user-agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0"
}
resp = requests.get(url,headers=head)
resp.encoding = 'gbk'
txt = resp.text
# print(txt)
# 提取代码中的href信息
# <ul class="pic-list2 clearfix"> <li class="photo-list-padding">
et = etree.HTML(txt)
result = et.xpath('//ul[@class="pic-list2 clearfix"]/li/a/@href')
print(result)
#输出结果
#['https://down10.zol.com.cn/desktoptools/XZDesktop_5018_3.1.3.6.exe', #'//desk.zol.com.cn/bizhi/9109_111583_2.html', '/bizhi/8676_107002_2.html', #'/bizhi/8530_105480_2.html', '/bizhi/8376_103851_2.html', #'/bizhi/8365_103747_2.html', '/bizhi/8339_103479_2.html', #'/bizhi/8336_103439_2.html', '/bizhi/8317_103216_2.html', #'/bizhi/8287_102877_2.html', '/bizhi/8286_102865_2.html', #'/bizhi/8280_102793_2.html', '/bizhi/8264_102617_2.html', #'/bizhi/8263_102613_2.html', '/bizhi/8261_102593_2.html', #'/bizhi/8260_102585_2.html', '/bizhi/8246_102427_2.html', #'/bizhi/8245_102410_2.html', '/bizhi/8242_102394_2.html', #'/bizhi/8234_102309_2.html', '/bizhi/8231_102269_2.html']
但是现在的链接无法打开图片,缺少域名
所以,加上:
domin = "https://desk.zol.com.cn" for item in result:url = domin+itemprint(url)
所有代码:
import requests
from lxml import etree
# 提取源代码
url = "https://desk.zol.com.cn/dongman/good_1.html"
head = {
"user-agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0"
}
resp = requests.get(url,headers=head)
resp.encoding = 'gbk'
txt = resp.text
print(txt)
# 提取代码中的href信息
# <ul class="pic-list2 clearfix"> <li class="photo-list-padding">
et = etree.HTML(txt)
result = et.xpath('//ul[@class="pic-list2 clearfix"]/li/a/@href')
print(result)
domin = "https://desk.zol.com.cn"
for item in result:url = domin+itemprint(url)