因爲缺乏清晰度(我是編程新手),我很抱歉。我試圖用lxml.etree解析一組本地文件。我使用lxml(和xpath)編寫了一個解析腳本,該腳本從SEC網頁中查找相關數據並導出到.csv文件。該腳本適用於單個url,但我想推廣到數千個html頁面。我已經獲得了本地下載的所有html文件(我使用curl獲取鏈接,wget下載) - 但是我沒有取代任何解析器的成功。該工作的老版本是:用lxml.etree解析本地文件
page = requests.get('url')
tree = html.fromstring(page.text)
我試着調用etree.parse來取代它,這樣我在目錄中「靶心」下載到本地解析文件
path = "/Users/dbk13/Desktop/SEC/bullseye"
dirs = os.listdir(path)
for files in dirs:
page = os.path.join(path,files)
etree.parse(page)
是否有問題與我的路徑本地文件?
我不斷收到的錯誤是一樣的東西:
File "postings_up_updated.py", line 26, in etree.parse(page) File "lxml.etree.pyx", line 3299, in lxml.etree.parse (src/lxml/lxml.etree.c:72421) File "parser.pxi", line 1791, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:105883) File "parser.pxi", line 1817, in lxml.etree._parseDocumentFromURL (src/lxml/lxml.etree.c:106182) File "parser.pxi", line 1721, in lxml.etree._parseDocFromFile (src/lxml/lxml.etree.c:105181) File "parser.pxi", line 1122, in lxml.etree._BaseParser._parseDocFromFile (src/lxml/lxml.etree.c:100131) File "parser.pxi", line 580, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:94254) File "parser.pxi", line 690, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:95690) File "parser.pxi", line 620, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:94757) lxml.etree.XMLSyntaxError: Document is empty, line 1, column 1
空文件不是有效的XML文檔!顯然,在'/ Users/dbk13/Desktop/SEC/bullseye'中有一個空文件。 – 2015-02-07 10:39:52