【发布时间】:2016-08-31 14:35:37
【问题描述】:
我正在尝试从以下评论中的网页“07/18/16”中提取日期。我不清楚 xpath 的语法,你如何只获取日期?
#<p>Opened <a class="timeline" href="/trac3/timeline?from=2016-07-
#18T14%3A46%3A43-04%3A00&precision=second" title="See timeline at
#07/18/16 14:46:43">6 weeks ago</a></p>
from lxml import html
import requests
page = requests.get(webpage)
tree = html.fromstring(page.content)
openDate = tree.xpath('//Opened/text()')
print 'Open Date: ', openDate
【问题讨论】:
-
在 # 上拆分标题一次,然后提取第二个元素并再次拆分第一个元素,使用 /a/@title 代替文本调用来获取标题
-
实际上 Opened 是 p 中的文本,所以你的 xpath 什么也找不到
标签: python regex xpath python-requests lxml