Python,将请求的结果解析为xml

时间:2018-05-30 08:09:36

标签: python xml parsing beautifulsoup

我正在尝试使用BeautifulSoup将请求的结果解析为xml。

但是,它返回错误消息:“TypeError:'Response'类型的对象没有len()”

这是我的代码:

r = requests.get(url, proxies=proxies, timeout=10)
result = BeautifulSoup(r,'html.parser')

4 个答案:

答案 0 :(得分:0)

将回复的内容传递给BeautifulSoup。

<强>实施例

result = BeautifulSoup(r.text,'html.parser')

答案 1 :(得分:0)

根据previous answer对类似的问题:

  

您将获得response.content。但它将响应体返回为字节(docs)。但是你应该将str传递给BeautifulSoup构造函数(docs)。因此,您需要使用response.text而不是获取内容。

答案 2 :(得分:0)

将r替换为BeautifulSoup函数中的r.text:

r = requests.get(url, proxies=proxies, timeout=10)

result = BeautifulSoup(r.text,'html.parser')

希望它有所帮助。快乐的编码:)

答案 3 :(得分:0)

在BeautifulSoup中,html.parser用于解析HTML内容,如果要解析XML内容,请使用lxml的XML解析器,如下所示:

result = BeautifulSoup(r,'lxml-xml')   # method 1
result = BeautifulSoup(r,'xml')        # method 2

在使用它们之前,您需要确保已安装lxml解析器。使用pip来安装它很容易。

  

pip install lxml