我在Python中使用lxml
来抓取网页。然而,为了得到表格的行数,我首先得到它们,然后使用len()
函数。我觉得这很浪费,有没有其他方法可以让他们的数字(动态的)进一步刮擦?lxml网页解析内容的长度
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)
为什么'beautifulsoup'标签?你只在这里使用'lxml'。 –
对不起,我以为可以用bs代替即兴 –