Xpath
XPath,全称 XML Path Language,即 XML 路径语言,它是一门在XML文档中查找信息的语言。XPath 最初设计是用来搜寻XML文档的,但是它同样适用于 HTML 文档的搜索。
1.XPath概览
- XPath 的选择功能十分强大,它提供了非常简洁明了的路径选择表达式,另外它还提供了超过 100 个内建函数用于字符串、数值、时间的匹配以及节点、序列的处理等等,几乎所有我们想要定位的节点都可以用XPath来选择。
XPath 于 1999 年 11 月 16 日 成为 W3C 标准,它被设计为供 XSLT、XPointer 以及其他 XML 解析软件使用,更多的文档可以访问其官方网站:https://www.w3.org/TR/xpath/。2.XPath常用规则
- nodename选取此节点的所有子节点
- /从当前节点选取直接子节点
- //从当前节点选取子孙节点
- .选取当前节点
- …选取当前节点的父节点
- @选取属性
在这里列出了XPath的常用匹配规则,例如 / 代表选取直接子节点,// 代表选择所有子孙节点,. 代表选取当前节点,…代表选取当前节点的父节点,@ 则是加了属性的限定,选取匹配属性的特定节点。
例如:
handlebars //title[@lang=’eng’]
这就是一个 XPath 规则,它就代表选择所有名称为 title,同时属性 lang 的值为 eng 的节点。
所以在做爬虫时,我们完全可以使用 XPath 来做相应的信息抽取,本节我们来以获取电影网站影片信息为例来了解 XPath 的基本用法。
准备工作
在使用之前我们首先要确保安装好了 LXML 库,可以导入 LXML 库的 etree 模块
from lxml import etree
实例引入
我们以下面这个网页为例
https://dytt8.net/html/gndy/dyzz/index.html
可以看到在当前最新电影专栏,共有245页,我们的目标是提取首页中电影《甘草披萨》相关信息。
首先,将首页url引入,并写入浏览器请求头:
base_url = "https://dytt8.net/html/gndy/dyzz/list_23_{0}.html"
#list_23_{0}表示从列表第一页开始
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36"
}
定义一个方法,用于把响应的内容按照gbk形式解码。
def download_html(url):
response = requests.get(url,headers=headers)
# 把响应的内容按照gbk形式解码
html_str = response.content.decode("gbk")
return html_str
我们通过Xpath表达式先来获取最新电影所有页面的URL
页面右击鼠标点检查,通过分析,用Xpath表达式返回内容是 [‘244’] 列表结果,列表中装载了字符串页码
first_url = base_url.format("1")
first_html_content = download_html(first_url)
first_content = etree.HTML(first_html_content) # 通过这个方法能够将html文档转换为能够执行xpath表达式的对象
# 获取最大页码
page_number_str = first_content.xpath("//select[@name='sldd']/option[last()]/text()")
# 返回的内容是 ['244'] 列表结果,列表中装载了字符串页码
# 拼接URL,获取到所有页面的URL
urls = []
for page_number in range(1,int(page_number_str[0])+1):
url = base_url.format(page_number)
urls.append(url)
获取到所有页面的URL之后,就可以解析每页的电影的超链接,我们只取第一页。
接下来,需要定义一个方法通过电影列表页面URL获取页面中电影超链接列表,在html中通过Xpath表达式定位到了当前页所有电影超链接
def get_movie_href_page_url(url):
html_str = download_html(url)
html_content = etree.HTML(html_str)
movie_second_hrefs = html_content.xpath("//table[@class='tbspan']//a/@href")
#在html中通过Xpath表达式定位到了当前页所有电影超链接
return movie_second_hrefs
拼接域名,获取完整url:
for url in urls[0:1]:#切片(只获取一页)
# 根据URL获取当前页面的电影href列表
movie_second_hrefs = get_movie_href_page_url(url)
# URL需要拼接上网站的域名
movie_second_hrefs = ["https://dytt8.net"+i for i in movie_second_hrefs]
#print(movie_second_hrefs)#输出当前页电影超链接
for movie_href in movie_second_hrefs[0:1]:#切片只取当前页第一个电影超链接
# 这个方法用来解析电影详情页面
get_movie_detail(movie_href)
最后就需要定义一个方法来解析《甘草披萨》这个电影详情页的信息
def get_movie_detail(href):
html_str = download_html(href)
html_content = etree.HTML(html_str)
movie = {} # 每一个电影信息用字典保存(将来这个结构的数据存储到数据库) csv中
title = html_content.xpath("//div[@class='title_all']//font/text()")#获取电影标题
movie['title:'] = title[0] if title and len(title) >= 1 else ""
image_src = html_content.xpath("//div[@id='Zoom']//img/@src")#获取电影宣传图片
movie['image_src:'] = image_src[0] if image_src and len(image_src) >= 1 else ""
movie_text = html_content.xpath("//div[@id='Zoom']//text()")#获取影片详情
# print(movie_text)
#下面对详情内容通过条件控制进行需求性输出
for info in movie_text:
#注意,此控制条件只对不换行字符串内容进行全部提取
if info.startswith("◎译 名"):
s = info.replace("◎译 名","").strip()
movie['transfer_name:'] = s
elif info.startswith("◎片 名"):
s = info.replace("◎片 名","").strip()
movie['title_name:'] = s
elif info.startswith("◎年 代"):
s = info.replace("◎年 代","").strip()
movie['age:'] = s
elif info.startswith("◎产 地"):
s = info.replace("◎产 地","").strip()
movie['producing_area:'] = s
elif info.startswith("◎类 别"):
s = info.replace("◎类 别","").strip()
movie['sort:'] = s
elif info.startswith("◎语 言"):
s = info.replace("◎语 言","").strip()
movie['language:'] = s
elif info.startswith("◎上映日期"):
s = info.replace("◎上映日期","").strip()
movie['release_date:'] = s
elif info.startswith("◎豆瓣评分"):
s = info.replace("◎豆瓣评分","").strip()
movie['rating:'] = s
elif info.startswith("◎片 长"):
s = info.replace("◎片 长","").strip()
movie['film_length:'] = s
elif info.startswith("◎导 演"):
s = info.replace("◎导 演","").strip()
movie['director:'] = s
elif info.startswith("◎编 剧"):
s = info.replace("◎编 剧","").strip()
movie['scriptwriter:'] = s
elif info.startswith("◎主 演"):
s = info.replace("◎主 演","").strip()
movie['actor:'] = s
elif info.startswith(" 这部影片"):
s = info.replace(" ","").strip()
movie['introductory:'] = s
for key,value in movie.items():
print(key,value)
源码分享
from cmath import inf
from ctypes import memmove
from lxml import etree
import requests
base_url = "https://dytt8.net/html/gndy/dyzz/list_23_{0}.html"
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36"
}
#方法
def download_html(url):
response = requests.get(url,headers=headers)
# 把响应的内容按照gbk形式解码
html_str = response.content.decode("gbk")
return html_str
# 通过每页电影列表页面URL获取页面中电影超链接列表
def get_movie_href_page_url(url):
html_str = download_html(url)
html_content = etree.HTML(html_str)
movie_second_hrefs = html_content.xpath("//table[@class='tbspan']//a/@href")
return movie_second_hrefs
# 通过每个电影的页面的URL来提取详细信息
def get_movie_detail(href):
html_str = download_html(href)
html_content = etree.HTML(html_str)
movie = {} # 每一个电影信息用字典保存(将来这个结构的数据存储到数据库) csv中
title = html_content.xpath("//div[@class='title_all']//font/text()")
movie['title:'] = title[0] if title and len(title) >= 1 else ""
image_src = html_content.xpath("//div[@id='Zoom']//img/@src")
movie['image_src:'] = image_src[0] if image_src and len(image_src) >= 1 else ""
movie_text = html_content.xpath("//div[@id='Zoom']//text()")
# print(movie_text)
for info in movie_text:
if info.startswith("◎译 名"):
s = info.replace("◎译 名","").strip()
movie['transfer_name:'] = s
elif info.startswith("◎片 名"):
s = info.replace("◎片 名","").strip()
movie['title_name:'] = s
elif info.startswith("◎年 代"):
s = info.replace("◎年 代","").strip()
movie['age:'] = s
elif info.startswith("◎产 地"):
s = info.replace("◎产 地","").strip()
movie['producing_area:'] = s
elif info.startswith("◎类 别"):
s = info.replace("◎类 别","").strip()
movie['sort:'] = s
elif info.startswith("◎语 言"):
s = info.replace("◎语 言","").strip()
movie['language:'] = s
elif info.startswith("◎上映日期"):
s = info.replace("◎上映日期","").strip()
movie['release_date:'] = s
elif info.startswith("◎豆瓣评分"):
s = info.replace("◎豆瓣评分","").strip()
movie['rating:'] = s
elif info.startswith("◎片 长"):
s = info.replace("◎片 长","").strip()
movie['film_length:'] = s
elif info.startswith("◎导 演"):
s = info.replace("◎导 演","").strip()
movie['director:'] = s
elif info.startswith("◎编 剧"):
s = info.replace("◎编 剧","").strip()
movie['scriptwriter:'] = s
elif info.startswith("◎主 演"):
s = info.replace("◎主 演","").strip()
movie['actor:'] = s
elif info.startswith(" 这部影片"):
s = info.replace(" ","").strip()
movie['introductory:'] = s
for key,value in movie.items():
print(key,value)
#print(movie)
# 看程序要从main方法去看
def main():
first_url = base_url.format("1")
first_html_content = download_html(first_url)
first_content = etree.HTML(first_html_content) # 通过这个方法能够将html文档转换为能够执行xpath表达式的对象
# 获取最大页码
page_number_str = first_content.xpath("//select[@name='sldd']/option[last()]/text()")
# 返回的内容是 ['244'] 列表结果,列表中装载了字符串页码
# 拼接URL,获取到所有页面的URL
urls = []
for page_number in range(1,int(page_number_str[0])+1):
url = base_url.format(page_number)
urls.append(url)
# 获取到所有页面的URL之后,解析每页的电影的超链接
for url in urls[0:1]:#切片(只获取一页)
# 根据URL获取当前页面的电影href列表
movie_second_hrefs = get_movie_href_page_url(url)
# URL需要拼接上网站的域名
movie_second_hrefs = ["https://dytt8.net"+i for i in movie_second_hrefs]
#print(movie_second_hrefs)#输出当前页电影超链接
for movie_href in movie_second_hrefs[0:1]:
# 这个方法用来解析电影详情页面
get_movie_detail(movie_href)
if __name__ == "__main__":
main()
效果展示