|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import requests
- import urllib
- import re
- from lxml import etree
- url = "http://zj.offcn.com/zg/skzcj/"
- url_html = requests.get(url).text
- treeurl = etree.HTML(url_html)
- #final_url = etree.tostring(treeurl).decode('utf-8')
- condition = "//div[@class='xc'][1]//a[1]/@href"
- xx = treeurl.xpath(condition)
- print(xx)
复制代码
以下是输出:
['http://zj.offcn.com/html/2018/03/225510.html',
'http://zj.offcn.com/html/2018/03/225509.html',
'http://zj.offcn.com/html/2018/03/225508.html',
'http://zj.offcn.com/html/2018/03/225507.html',
'http://zj.offcn.com/html/2018/03/225506.html',
'http://zj.offcn.com/html/2018/03/225505.html',
'http://zj.offcn.com/html/2018/03/225504.html',
'http://zj.offcn.com/html/2018/03/225503.html',
'http://zj.offcn.com/html/2018/03/225502.html',
'http://zj.offcn.com/html/2018/03/225501.html',
'http://zj.offcn.com/html/2018/03/225500.html',
'http://zj.offcn.com/html/2018/03/225499.html',
'http://zj.offcn.com/html/2018/03/225498.html']
为什么选出这么多条?我明明写了a[1]
- condition = "//div[@class='xc'][1]//a[1]/@href"
复制代码
此处的下一级应该是: - //div[@class='xc'][1]//li[1]/a/@href
复制代码
非常欣赏楼主自己写xpath路径 !要是觉得自己写的稍微有点出入的话,可以借助浏览器的xpath插件来对比验证一下
|
|