如何在urllib中使用xpath表达式

版权声明:https://blog.csdn.net/qq_43546676 https://blog.csdn.net/qq_43546676/article/details/89046085

一、安装lxml模块

(1)按Ctrl + R键,然后输入cmd,这样进入了黑窗口。
(2)输入以下代码:

 pip install lxml

(3)等待安装完成

二、实例演示

 import urllib.request
from lxml import etree

data = urllib.request.urlopen('https://www.baidu.com').read().decode('utf-8', 'ignore')
title = etree.HTML(data).xpath('/title/text()')
# 这样直接获取的title时候不是一个列表,是一个可迭代对象,所以转换一下
if str(type(title)) != "<class 'list'>":
    title = list(title)
print(title)

猜你喜欢

转载自blog.csdn.net/qq_43546676/article/details/89046085
今日推荐