首页 > Web开发 > 详细

6、通过xpath获取网页数据

时间:2018-03-21 16:56:45      阅读:163      评论:0      收藏:0      [点我收藏+]
from urllib import request
from lxml import etree
# 请求的url
url = "http://www.dfenqi.cn/Product/Index"
# 请求的头文件
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
}
# 创建请求对象
req = request.Request(url,headers = headers)
# 创建处理器对象
httpHandler = request.HTTPHandler()
# 创建opener
opener = request.build_opener(httpHandler)
# 发送请求
response = opener.open(req)
# 读取源文件
html = response.read().decode(‘utf-8‘)
# 创建xpath关系
xpath = "//div[@class=‘liebiao‘]/ul/li/p/text()"
# 获取属性值列表
# xpath = "//div[@class=‘liebiao‘]/ul/li/p/@class"
# 将html转换成可解析对象
selector = etree.HTML(html)
# 返回xpath查询列表
goodsList = selector.xpath(xpath)
# 显示商品标题
for goods in goodsList:
    print(goods)

6、通过xpath获取网页数据

原文:https://www.cnblogs.com/toloy/p/8618007.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!