如何在scrapy中将字符串转换为响应对象?

时间:2015-04-24 18:12:46

标签: python scrapy

我想转换包含html标记的字符串,以便我可以使用selectors来提取少量信息。我在html回复中收到此json。我使用了HTMLParser,但收到的错误为exceptions.TypeError: cannot create weak reference to 'unicode' object

我的代码是:

sel = Selector(response)
jsonresponse = json.loads(response.body_as_unicode())
hello= jsonresponse["html"].encode('ascii','ignore')
h = HTMLParser.HTMLParser()
world=h.unescape(hello)
sel1 = Selector(world)

1 个答案:

答案 0 :(得分:0)

我得到了答案:

我只需在使用text时提及Selectors,以防案例响应为字符串。

sel = Selector(response)
jsonresponse = json.loads(response.body_as_unicode())
hello= jsonresponse["html"].encode('ascii','ignore')
sel1 = Selector(text=hello)