我正在使用Python Scrapy。我想从没有 HTML 标签的网页中提取文本。下面是我的代码(从此页面获得想法:如何使用Scrapy从网站获取所有纯文本?
sel = Selector(response)
item = DeletespiderItem()
item['url'] = response.url
description = sel.select("//body").extract()
tree = lxml.html.fromstring(description)
item['description'] = tree.text_content().strip()
yield item
但是我收到以下错误
File "C:Python27libsite-packageslxmlhtml__init__.py", line 722, in fromstring
is_full_html = _looks_like_full_html_unicode(html)
exceptions.TypeError: expected string or buffer
我的代码出了什么问题。如何从中获取纯文本?
谁能帮我?谢谢
更新:
Scapy shell https://stackoverflow.com/questions/23156780/how-can-i-get-all-the-plain-text-from-a-website-with-scrapy
sel.select("//body").extract()[0].strip()
o/p \r \r \r \r \r \rchat\r ]
它正在添加额外的 \r ?
extract()
返回一个列表,请使用:
description = sel.select("//body").extract()[0]