我在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代替即興 –