lxml
我用Python抓取网页。然而,要获取表格行数,我首先将它们全部获取,然后使用len()
函数。我觉得这很浪费,还有其他方法可以让他们的号码(动态号码)进一步刮吗?
import lxml.html
doc = ''
try:
doc = lxml.html.parse('url')
except SkipException: pass
if doc:
buf = ''
#get the total number of rows in table
tr = doc.xpath("/html/body/div[1]/div[1]/table[1]/tbody/tr")
table = []
# iterate over the table rows limited to max number
for i in range(3, len(tr)):
# get the rows content
table += doc.xpath("body/div[1]/div[1]/table[1]/tbody/tr[%s]/td" % i)