我有这样的html:我需要使用当前时间列旁边的上升/下降列[第10列]来获取当前时间!
<table id="table" class="tablesorter">
<thead>
<tr>
<th rowspan="2"><div align="center">Sno</div></th>
<th rowspan="2"><div align="center">Site Id</div></th>
<th rowspan="2"><div align="center">Mandal</div></th>
<th rowspan="2"><div align="center">Piezometer
Location
(Village) </div></th>
<th rowspan="2" ><div align="center">July-18
15/05/2018 <br>10:00 HRS</div></th>
<th rowspan="2" ><div align="center">Nov-18</div></th>
<th rowspan="2" ><div align="center">May-19</div></th>
<th rowspan="2" ><div align="center">June-19</div></th>
<th rowspan="2" ><div align="center">July-19
15/07/2019 <br>10:00 HRS</div></th>
<th colspan="4" ><div align="center">Rise(+)/Fall(-) from current water level
and with reference to</div></th>
</tr>
<tr>
<th ><div align="center">July-18</div></th>
<th ><div align="center">Nov-18</div></th>
<th ><div align="cesnter">May-19</div></th>
<th ><div align="cesnter">Jun-19</div></th>
</tr>
</thead>
<tbody>
<div align="center">
我的目标是获取当前时间,该时间位于“上升/下降”列之前。这是我写的代码
import requests
from lxml import html
url = 'http://www.apsdps.gov.in/gw_status.jsp?s1=1'
def scrape():
print("start round")
try:
r=requests.get(url)
d=r.content.decode(r.encoding)
tree=html.fromstring(d)
table = tree.xpath("//table[@id='table']")[0]
fq_time_ele = tree.xpath("//table[@id='table']//thead//th//[contains(text(),'Rise(+)/Fall(-) from current water level and with reference to')]//preceding-sibling::th[1]//text()")
curdate = fq_time_ele[0].strip().split()[-1].replace("/", "-")
curtime = fq_time_ele[1].split(" ")[0].split(":")[0]
time_str = curdate + "_" + curtime
print(time_str)
except Exception as e:
print("Error ", str(e))
print("end round")
try:
scrape()
except:
print("It is not working")
我需要当前时间,但是代码不起作用。谁能帮我吗?
答案 0 :(得分:1)
对更正后的xpath使用以下方法:
import requests
from lxml import html
url = 'http://www.apsdps.gov.in/gw_status.jsp?s1=1'
def scrape():
print("start round")
try:
content = requests.get(url).content
tree = html.fromstring(content)
curr_time_parts = tree.xpath("//table[@id='table']//th[*[contains(text(),'Rise(+)/Fall(-)')]]"
"/preceding-sibling::th[1]/*/text()")
date_, time_ = curr_time_parts
date_ = ' '.join(date_.split())
print(date_, time_)
except Exception as e:
print("Error ", str(e))
print("end round")
try:
scrape()
except:
print("It is not working")
输出:
start round
July-19 16/07/2019 16:00 HRS
end round