擅长:python、mysql、java
<p>所以多亏了@CodeNinja的帮助,我才稍微调整了一下,才得到我想要的东西。
我导入时间来获取运行代码的日期。不管怎样,这是我想要的代码。谢谢你的帮助!!在</p>
<pre><code>import lxml.html
import time
url = "http://gbgfotboll.se/information/?scr=table&ftid=51168"
html = lxml.html.parse(url)
currentDate = (time.strftime("%Y-%m-%d"))
for i in range(12):
xpath1 = ".//*[@id='content-primary']/table[3]/tbody/tr[%d]/td[1]/span/span//text()" %(i+1)
xpath2 = ".//*[@id='content-primary']/table[3]/tbody/tr[%d]/td[2]/a/text()" %(i+1)
time = html.xpath(xpath1)[1]
date = html.xpath(xpath1)[0]
teamName = html.xpath(xpath2)[0]
if date == currentDate:
print time, teamName
</code></pre>
<p>所以这里是如何正确操作的最终版本。这将解析它拥有的所有表行,而不在for循环中使用“range”。我从我的另一个帖子得到了这个答案:<a href="https://stackoverflow.com/questions/25964194/iterate-through-all-the-rows-in-a-table-using-python-lxml-xpath">Iterate through all the rows in a table using python lxml xpath</a></p>
^{pr2}$