什么是XPath?
xpath(XML Path Language)是一门在XML和HTML文档中查找信息的语言,可用来在XML和HTML文档中对元素和属性进行遍历。
XPath开发工具
- Chrome插件XPath Helper。
- Firefox插件Try XPath。
XPath语法
选取节点:
XPath 使用路径表达式来选取 XML 文档中的节点或者节点集。这些路径表达式和我们在常规的电脑文件系统中看到的表达式非常相似。
表达式 | 描述 | 示例 | 结果 |
---|---|---|---|
nodename | 选取此节点的所有子节点 | bookstore | 选取bookstore下所有的子节点 |
/ | 如果是在最前面,代表从根节点选取。否则选择某节点下的某个节点 | /bookstore | 选取根元素下所有的bookstore节点 |
// | 从全局节点中选择节点,随便在哪个位置 | //book | 从全局节点中找到所有的book节点 |
@ | 选取某个节点的属性 | //book[@price] | 选择所有拥有price属性的book节点 |
. | 当前节点 | ./a | 选取当前节点下的a标签 |
谓语:
谓语用来查找某个特定的节点或者包含某个指定的值的节点,被嵌在方括号中。
在下面的表格中,我们列出了带有谓语的一些路径表达式,以及表达式的结果:
路径表达式 | 描述 |
---|---|
/bookstore/book[1] | 选取bookstore下的第一个子元素 |
/bookstore/book[last()] | 选取bookstore下的倒数第二个book元素。 |
bookstore/book[position()<3] | 选取bookstore下前面两个子元素。 |
//book[@price] | 选取拥有price属性的book元素 |
//book[@price=10] | 选取所有属性price等于10的book元素 |
//book[contains(@class,'name')] | 选取所有book元素下class属性包含有name参数 |
通配符
*表示通配符。
通配符 | 描述 | 示例 | 结果 |
---|---|---|---|
* | 匹配任意节点 | /bookstore/* | 选取bookstore下的所有子元素。 |
@* | 匹配节点中的任何属性 | //book[@*] | 选取所有带有属性的book元素。 |
选取多个路径:
通过在路径表达式中使用“|”运算符,可以选取若干个路径。
示例如下:
//bookstore/book | //book/title
# 选取所有book元素以及book元素下所有的title元素
运算符:
lxml库
lxml 是 一个HTML/XML的解析器,主要的功能是如何解析和提取 HTML/XML 数据。
lxml和正则一样,也是用 C 实现的,是一款高性能的 Python HTML/XML 解析器,我们可以利用之前学习的XPath语法,来快速的定位特定元素以及节点信息。
lxml python 官方文档:http://lxml.de/index.html
需要安装C语言库,可使用 pip 安装:pip install lxml
基本使用:
我们可以利用他来解析HTML代码,并且在解析HTML代码的时候,如果HTML代码不规范,他会自动的进行补全。示例代码如下:
# 使用 lxml 的 etree 库
from lxml import etree
text = '''
<div>
<ul>
<li class="item-0"><a href="link1.html">first item</a></li>
<li class="item-1"><a href="link2.html">second item</a></li>
<li class="item-inactive"><a href="link3.html">third item</a></li>
<li class="item-1"><a href="link4.html">fourth item</a></li>
<li class="item-0"><a href="link5.html">fifth item</a> # 注意,此处缺少一个 </li> 闭合标签
</ul>
</div>
'''
#利用etree.HTML,将字符串解析为HTML文档
html = etree.HTML(text)
# 按字符串序列化HTML文档
result = etree.tostring(html)
print(result)
输入结果如下:
<html><body>
<div>
<ul>
<li class="item-0"><a href="link1.html">first item</a></li>
<li class="item-1"><a href="link2.html">second item</a></li>
<li class="item-inactive"><a href="link3.html">third item</a></li>
<li class="item-1"><a href="link4.html">fourth item</a></li>
<li class="item-0"><a href="link5.html">fifth item</a></li>
</ul>
</div>
</body></html>
可以看到。lxml会自动修改HTML代码。例子中不仅补全了li标签,还添加了body,html标签。
从文件中读取html代码:
除了直接使用字符串进行解析,lxml还支持从文件中读取内容。我们新建一个hello.html文件:
<!-- hello.html -->
<div>
<ul>
<li class="item-0"><a href="link1.html">first item</a></li>
<li class="item-1"><a href="link2.html">second item</a></li>
<li class="item-inactive"><a href="link3.html"><span class="bold">third item</span></a></li>
<li class="item-1"><a href="link4.html">fourth item</a></li>
<li class="item-0"><a href="link5.html">fifth item</a></li>
</ul>
</div>
然后利用etree.parse()
方法来读取文件。示例代码如下:
from lxml import etree
# 读取外部文件 hello.html
html = etree.parse('hello.html')
result = etree.tostring(html, pretty_print=True)
print(result)
输入结果和之前是相同的。
在lxml中使用XPath语法:
-
获取所有li标签:
from lxml import etree html = etree.parse('hello.html') print type(html) # 显示etree.parse() 返回类型 result = html.xpath('//li') print(result) # 打印<li>标签的元素集合
-
获取所有li元素下的所有class属性的值:
from lxml import etree html = etree.parse('hello.html') result = html.xpath('//li/@class') print(result)
-
获取li标签下href为
www.baidu.com
的a标签:from lxml import etree html = etree.parse('hello.html') result = html.xpath('//li/a[@href="www.baidu.com"]') print(result)
-
获取li标签下所有span标签:
from lxml import etree html = etree.parse('hello.html') #result = html.xpath('//li/span') #注意这么写是不对的: #因为 / 是用来获取子元素的,而 <span> 并不是 <li> 的子元素,所以,要用双斜杠 result = html.xpath('//li//span') print(result)
-
获取li标签下的a标签里的所有class:
from lxml import etree html = etree.parse('hello.html') result = html.xpath('//li/a//@class') print(result)
-
获取最后一个li的a的href属性对应的值:
from lxml import etree html = etree.parse('hello.html') result = html.xpath('//li[last()]/a/@href') # 谓语 [last()] 可以找到最后一个元素 print(result)
-
获取倒数第二个li元素的内容:
from lxml import etree html = etree.parse('hello.html') result = html.xpath('//li[last()-1]/a') # text 方法可以获取元素内容 print(result[0].text)
-
获取倒数第二个li元素的内容的第二种方式:
from lxml import etree html = etree.parse('hello.html') result = html.xpath('//li[last()-1]/a/text()') print(result)
chrome相关问题:
在62版本(目前最新)中有一个bug,在页面302重定向的时候不能记录FormData数据。这个是这个版本的一个bug。详细见以下链接:https://stackoverflow.com/questions/34015735/http-post-payload-not-visible-in-chrome-debugger。
在金丝雀版本中已经解决了这个问题,可以下载这个版本继续,链接如下:https://www.google.com/chrome/browser/canary.html
实战-豆瓣电影爬虫:
import requests
from lxml import etree
# 1.将目标网站的页面抓取下来
headers = {
'Host':'movie.douban.com',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
'Referer':'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&srcqid=2328577874318033697&tn=93006350_hao_pg&wd=douban&oq=%25E8%2585%25BE%25E8%25AE%25AF%25E6%258B%259B%25E8%2581%2598&rsv_pq=a5c9fad500032d39&rsv_t=c45fuFhfw1QXFIl989itMrzlcssxBzOrrVGSndzUMM1KcQQn7C8JY6zoz3pfqLSKLuTd0%2FO8&rqlang=cn&rsv_enter=1&inputT=5137&rsv_sug3=33&rsv_sug2=0&rsv_sug4=5138'
}
url = 'https://movie.douban.com/'
resp = requests.get(url=url,headers=headers)
text = resp.text
# 2.将抓取下来的数据根据一点的规则进行提取
parser = etree.HTMLParser(encoding='utf-8')
html = etree.HTML(text,parser=parser)
ul = html.xpath("//ul[@class='ui-slide-content']")[0]
lis = ul.xpath("./li")
positions = []
for li in lis:
url = li.xpath(".//li[@class='poster']/a/@href")[0]
payUrl = li.xpath(".//li[@class='ticket_btn']//a/@href")[0]
img = li.xpath(".//img/@src")[0]
title = li.xpath("@data-title")[0]
release = li.xpath("@data-release")[0]
rate = li.xpath("@data-rate")[0]
director = li.xpath("@data-director")[0]
actors = li.xpath("@data-actors")[0]
duration = li.xpath("@data-duration")[0]
region = li.xpath("@data-region")[0]
position = {
'url':url,
'payUrl':payUrl,
'img':img,
'title':title,
'release':release,
'rate':rate,
'director':director,
'actors':actors,
'duration':duration,
'region':region
}
positions.append(position)
print(positions)
实战-电影天堂爬虫:
from lxml import etree
import requests
# 分页爬取和详情页面
# BASE_URL = 'http://www.ygdy8.com/'
# url = 'http://www.ygdy8.com/html/gndy/dyzz/list_23_1.html'
# headers = {
# 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
# }
#
# resp = requests.get(url=url,headers=headers)
# text = resp.content.decode('gbk')
#
# parser = etree.HTMLParser(encoding='utf-8')
# html = etree.HTML(text,parser=parser)
# detail_urls = html.xpath("//table[@class='tbspan']//a/@href")
# for detail_url in detail_urls:
# print(BASE_URL+detail_url)
# 1. 先抓取每个页面的详情url
BASE_URL = 'http://www.ygdy8.com/'
HEADERS = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
}
def get_detail_urls(url):
resp = requests.get(url=url, headers=HEADERS)
#text = resp.content.decode('gbk')
text = resp.text
parser = etree.HTMLParser(encoding='utf-8')
html = etree.HTML(text, parser=parser)
detail_urls = html.xpath("//table[@class='tbspan']//a/@href")
# map将列表的每一项做相同的事情(等价于以下表达式)
# def abc(url):
# return BASE_URL+url
# index = 0
# for detail_url in detail_urls:
# detail_url = abs(detail_url)
# detail_urls[index] = detail_url
# index += 1
detail_urls = map(lambda url:BASE_URL+url,detail_urls)
return detail_urls
def parse_detail_page(url):
movies = {}
response = requests.get(url,headers=HEADERS)
text = response.content.decode('gbk')
html = etree.HTML(text)
title = html.xpath("//div[@class='title_all']//font/text()")[0]
movies['title'] = title
imgs = html.xpath("//div[@id='Zoom']//img")
if len(imgs) > 0:
if len(imgs) == 1:
cover = imgs[0]
movies['cover'] = cover
screenshot = ''
movies['screenshot'] = screenshot
if len(imgs) == 2:
cover = imgs[0]
movies['cover'] = cover
screenshot = imgs[1]
movies['screenshot'] = screenshot
infos = html.xpath("//div[@id='Zoom']//text()")
actors = []
abstracts = []
# for info in infos:
for index,info in enumerate(infos):
# print(info)
# print(index)
# startswith 函数是判断前面字符是否一样
if info.startswith('◎年 代'):
#replace 是替换函数 strip 将一个字符串的前后空字符全部删掉
year = info.replace('◎年 代','').strip()
movies['year'] = year
elif info.startswith('◎产 地'):
# replace 是替换函数 strip 将一个字符串的前后空字符全部删掉
country = info.replace('◎产 地', '').strip()
movies['country'] = country
elif info.startswith('◎类 别'):
# replace 是替换函数 strip 将一个字符串的前后空字符全部删掉
category = info.replace('◎类 别', '').strip()
movies['category'] = category
elif info.startswith('◎语 言'):
# replace 是替换函数 strip 将一个字符串的前后空字符全部删掉
language = info.replace('◎语 言', '').strip()
movies['language'] = language
elif info.startswith('◎主 演'):
# replace 是替换函数 strip 将一个字符串的前后空字符全部删掉
actor = info.replace('◎主 演', '').strip()
actors = [actor]
for x in range(index+1,len(infos)):
if infos[x].startswith('◎简 介 '):
break
actors.append(infos[x].strip())
movies['actors'] = actors
elif info.startswith('◎简 介'):
# replace 是替换函数 strip 将一个字符串的前后空字符全部删掉
# abstract = info.replace('◎简 介', '').strip()
for x in range(index + 1, len(infos)):
if infos[x].startswith('【下载地址】'):
break
abstract = infos[x].strip()
abstracts.append(abstract)
movies['abstract'] = abstracts
download_url = html.xpath("//div[@id='Zoom']//td[@style='WORD-WRAP: break-word']/a/@href")[0]
movies['download_url'] = download_url
return movies
def spider():
base_url = 'http://www.ygdy8.com/html/gndy/dyzz/list_23_{}.html'
for x in range(1,8):
url = base_url.format(x)
movie_detail_urls = get_detail_urls(url)
for movie_detail_url in movie_detail_urls:
movies = parse_detail_page(movie_detail_url)
print(movies)
if __name__ == '__main__':
spider()
实战抓取广西人才网最新的python工程师的招聘信息:
from lxml import etree
import requests
# 实战抓取广西人才网最新的python工程师的招聘信息
BASE_URL = 'http://s.gxrc.com/'
HEADERS = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
}
def spider():
base_url = 'http://s.gxrc.com/sJob?schType=1&pageSize=20&orderType=0&listValue=1&keyword=python&page={}'
for x in range(1,7):
url = base_url.format(x)
detail_urls = get_detail_urls(url)
for detail_url in detail_urls:
detail = parse_detail_page(detail_url)
print(detail)
def parse_detail_page(url):
detail = {}
response = requests.get(url,headers=HEADERS)
text = response.content.decode('gbk')
html = etree.HTML(text)
job = html.xpath("//div[@class='gsR_con']//h1[@id='positionName']/text()")[0]
detail['job'] = job.strip()
company = html.xpath("//div[@class='gsR_con']//a/text()")[0]
detail['job'] = company
infos = html.xpath("//div[@class='gsR_con']/table[@class='gs_zp_table']//td/text()")
for index,info in enumerate(infos):
if index == 0:
detail['num'] = info.strip()
elif index == 1:
detail['education'] = info.strip()
elif index == 2:
detail['wages'] = info.strip()
elif index == len(infos)-1:
detail['welfare'] = info.strip()
jobContentList = []
jobContents = html.xpath("//div[@class='gz_info_txt']//p/text()")
for jobContent in jobContents:
jobContentList.append(jobContent.strip())
print(jobContent)
detail['jobContentList'] = jobContentList
# num = html.xpath("//div[@class='gs_zp_table']//a/text()")[0]
# detail['num'] = num
# education = html.xpath("//div[@class='gs_zp_table']//a/text()")[0]
# detail['education'] = education
# wages = html.xpath("//div[@class='gsR_con']//a/text()")[0]
# detail['wages'] = wages
# welfare = html.xpath("//div[@class='gsR_con']//a/text()")[0]
# detail['welfare'] = welfare
# print(company)
return detail
def get_detail_urls(url):
resp = requests.get(url=url, headers=HEADERS)
text = resp.content.decode('utf-8')
parser = etree.HTMLParser(encoding='utf-8')
html = etree.HTML(text, parser=parser)
detail_urls = html.xpath("//div[@class='rlOne']//li[@class='w1']//a/@href")
# map将列表的每一项做相同的事情(等价于以下表达式)
# def abc(url):
# return BASE_URL+url
# index = 0
# for detail_url in detail_urls:
# detail_url = abs(detail_url)
# detail_urls[index] = detail_url
# index += 1
#detail_urls = map(lambda url:BASE_URL+url,detail_urls)
return detail_urls
if __name__ == '__main__':
spider()