如何在scrapy中将字符串转换为响应对象?(How to convert string into response object in scrapy?)

编程入门 行业动态 更新时间:2024-10-24 07:28:10
如何在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)

更多推荐

本文发布于:2023-07-05 08:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1035480.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   转换为   中将   对象   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!