如何在scrapy中将字符串转换为响应对象?(How to convert string into response object in scrapy?)
我想转换包含html标签的字符串,以便我可以使用selectors来提取少量信息。 我在json响应中得到这个html 。 我已经使用过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)I want to convert a string that contains html tags so that I can use selectors to extract few information. I'm getting this html in a json response. I've used HTMLParser but I'm getting an error as exceptions.TypeError: cannot create weak reference to 'unicode' object
My code is:
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)最满意答案
我得到了答案:
我只需要在使用Selectors提及text ,以防案例响应是一个字符串。
sel = Selector(response) jsonresponse = json.loads(response.body_as_unicode()) hello= jsonresponse["html"].encode('ascii','ignore') sel1 = Selector(text=hello)I got the answer:
I just needed to mention text while using Selectors in case response is a string.
sel = Selector(response) jsonresponse = json.loads(response.body_as_unicode()) hello= jsonresponse["html"].encode('ascii','ignore') sel1 = Selector(text=hello)更多推荐
发布评论