lxml错误“;IOError:读取文件时出错;在python脚本中解析facebook mobile时

lxml错误“;IOError:读取文件时出错;在python脚本中解析facebook mobile时,python,linux,facebook,web-scraping,lxml,Python,Linux,Facebook,Web Scraping,Lxml,我使用post中的修改脚本: 执行代码时,出现以下问题: $ ./facebook_fetch_coms.py Traceback (most recent call last): File "./facebook_fetch_coms_classic_test.py", line 42, in <module> tree = etree.parse(body) File "lxml.etree.pyx", line 2957, in lxml.etree.parse

我使用post中的修改脚本:

执行代码时,出现以下问题:

$ ./facebook_fetch_coms.py
Traceback (most recent call last):
  File "./facebook_fetch_coms_classic_test.py", line 42, in <module>
    tree = etree.parse(body)
  File "lxml.etree.pyx", line 2957, in lxml.etree.parse (src/lxml/lxml.etree.c:56230)
  File "parser.pxi", line 1533, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:82313)
  File "parser.pxi", line 1562, in lxml.etree._parseDocumentFromURL (src/lxml/lxml.etree.c:82606)
  File "parser.pxi", line 1462, in lxml.etree._parseDocFromFile (src/lxml/lxml.etree.c:81645)
  File "parser.pxi", line 1002, in lxml.etree._BaseParser._parseDocFromFile (src/lxml/lxml.etree.c:78554)
  File "parser.pxi", line 569, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:74498)
  File "parser.pxi", line 650, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:75389)
  File "parser.pxi", line 588, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:74691)
IOError: Error reading file '<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Facebook</title><meta name="description" content="Facebook helps you connect and share with the people in your life."
$。/facebook\u fetch\u coms.py
回溯(最近一次呼叫最后一次):
文件“/facebook\u fetch\u coms\u classic\u test.py”,第42行,在
tree=etree.parse(主体)
lxml.etree.parse(src/lxml/lxml.etree.c:56230)中第2957行的文件“lxml.etree.pyx”
文件“parser.pxi”,第1533行,在lxml.etree.\u parseDocument(src/lxml/lxml.etree.c:82313)中
文件“parser.pxi”,第1562行,位于lxml.etree.\u parseDocumentFromURL(src/lxml/lxml.etree.c:82606)
文件“parser.pxi”,第1462行,在lxml.etree.\u parseDocFromFile(src/lxml/lxml.etree.c:81645)中
文件“parser.pxi”,第1002行,在lxml.etree.\u BaseParser.\u parseDocFromFile(src/lxml/lxml.etree.c:78554)中
文件“parser.pxi”,第569行,位于lxml.etree.\u ParserContext.\u handleParseResultDoc(src/lxml/lxml.etree.c:74498)
文件“parser.pxi”,第650行,在lxml.etree.中。\u handleParseResult(src/lxml/lxml.etree.c:75389)
文件“parser.pxi”,第588行,在lxml.etree中。\u raiseParserError(src/lxml/lxml.etree.c:74691)
IOError:读取文件“”时出错
Facebook这是你的问题:

tree = etree.parse(body)
说明“
source
是包含XML数据的文件名或文件对象”。您提供了一个字符串,因此lxml将HTTP响应正文的文本作为要打开的文件的名称。不存在这样的文件,因此您会得到一个
IOError

您收到的错误消息甚至会说“错误读取文件”,然后给出您的XML字符串作为它试图读取的文件的名称,这是一个关于发生了什么的巨大提示

您可能需要,它从字符串中获取输入。或者你可以只做
tree=etree.parse(res)
直接从HTTP请求读入lxml(opener.open()
的结果是一个类似文件的对象,
etree.parse()
应该非常乐意使用它)。

我删除了
parse()
,取而代之的是
HTML()
,它工作得更好,谢谢。
tree = etree.parse(body)