如何在python中使用xpath
前门说了那么多xpath的语法,那么现在该如何使用呢。这里的话就离不开我们的lxml这个库里面的etree。这里要注意一下的时由于lxml时使用C语言编写的所以有些函数方法在pycharm中是没有提示的。
安装lxml
pip install lxml
直接通过pip来安装如果出现了一些问题的可以去查看我之前写的有关于pip安装的博客。
etree和xpath的联动(element)
用我们先前的上一篇博客的例子;Xpath 的语法小结我们继续以这个诗歌网站为例。
1 获取网页文本(html)
from lxml import etree
import requests
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
' AppleWebKit/537.36 (KHTML, like Gecko)'
' Chrome/80.0.3987.149 Safari/537.36'}
url='https://www.elanp.com/shige/'
text_=requests.get(url,headers=headers)
text=text_.content.decode('gb2312')
这里注意一下该网站时使用‘gb2312’编码的。
2.创建element用于使用xpath语法
html_=etree.HTML(text)
这里得到的是element的类可以使用xpath
这里我们就可以使用xpath语法了
比如:
date_1=html_.xpath('''//div[@class="mypos"]/a[@href="//www.elanp.com"/]text()''']
结果如下;
这里使用etree.HTML()不仅仅只是相当于桥梁使用xpath更重要的是它可以补全不完整的HTML代码。
我们可以将其打印出来。
htmlcode=etree.tostring(html_,encoding='utf-8').decode('utf-8')
#tostring()方法得到的是bytes数据我们需要将其转化为utf-8,
#同时其默认的编码是Unicode格式,所以要先转化为utf-8的格式之后解码。
之后较为完整的代码是:
from lxml import etree
import requests
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
' AppleWebKit/537.36 (KHTML, like Gecko)'
' Chrome/80.0.3987.149 Safari/537.36'}
url='https://www.elanp.com/shige/'
text_=requests.get(url,headers=headers)
text=text_.content.decode('gb2312')
html_=etree.HTML(text)
date_1=html_.xpath('''//div[@class="mypos"]/a[@href="//www.elanp.com/"]/text()''']
#htmlcode=etree.tostring(html_,encoding='utf-8').decode('utf-8')
#print(htmlcode)
print(date_1)
这里再把先前博客里的xpath语句执行一下;
- /html/head/title/text()
- //div[position()=1]/a[@href="//www.elanp.com/"]/text()
- //div[@class=“mypos”]/a[@href="//www.elanp.com/"]/text()
- //div[position()<2]/a[contains(@href,“w”)]/@href
- (由于输入的问题有些这里所显示的xpath语句存在中文符号请以代码中的为准)
结果如下:
代码如下:
from lxml import etree
import requests
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
' AppleWebKit/537.36 (KHTML, like Gecko)'
' Chrome/80.0.3987.149 Safari/537.36'}
url='https://www.elanp.com/shige/'
text_=requests.get(url,headers=headers)
text=text_.content.decode('gb2312')
html_=etree.HTML(text)
date_1=html_.xpath('''//div[@class="mypos"]/a[@href="//www.elanp.com/"]/text()''']
#htmlcode=etree.tostring(html_,encoding='utf-8').decode('utf-8')
#print(htmlcode)
#print(date_1)
def get_date(xpath_grammar):
try :
date_1=html_.xpath(xpath_grammar)
print(date_1)
except Exception as e:
print(e)
if __name__=='__main__':
value1='''/html/head/title/text()'''
value2='''//div[position()=1]/a[@href="//www.elanp.com/"]/text()'''
value3='''//div[@class="mypos"]/a[@href="//www.elanp.com/"]/text()'''
value4='''//div[position()<2]/a[contains(@href,"w")]/@href'''
get_date(value1)
get_date(value2)
get_date(value3)
get_date(value4)
从本地导入html文本。
这里的话只需要使用etree.parse(filename)即可
但是这里需要注意的是parse不会像HTML一样自动补全html代码所以有时候直接使用会报错。所以我们需要指定解释器。
我们可以看见它的__init__文件
因此:
- 创建解释器;parse=etree.HTMLParser(encoding=‘utf-8’)
- 指定解释器;html=etree.parse(filename,parse)
- xpath 操作; html.xpath(语句)
- 代码如下;
filename='本地文件名html'
parse=etree.HTMLParser(encoding='utf-8')
html=etree.parse(filename,parse)
#html_text=etree.tostring(html,encoding='utf-8').decode('utf-8')
SUMMARY
愉快的一天终于要结束了!!!