最近使用scrapy做数据采集,需要借助goose进行文本内容提取,提取数据部分的代码如下:
def parse(self, response, **kwargs):
g = Goose()
article = g.extract(raw_html=response.text)
item['title'] = article.title
item['author'] = ','.join(article.authors)
item['publish_time'] = article.publish_datetime_utc
item['content'] = article.cleaned_text
运行后出现如下错误:
AttributeError: 'list' object has no attribute 'decode'
在网上找了很久没有找到对应的解决办法。经过摸索,我得到如下两个解决方案:
1. 将传入html源码改为传入请求url
article = g.extract(url=response.url)
通过上述方式虽然能够解决问题,但再次请求会消耗更多资源,于是我找到了第二种方法。
2. 升级goose3
在终端中执行如下命令:
pip install --upgrade goose3
完成后即可正常运行。