[英]json encoded as UTF-8 characters. How do I process as json in Python Requests
I am scraping a website that is rendering a JavaScript/JSON Object that looks like this: 我正在抓取一个呈现如下JavaScript / JSON对象的网站:
{ "company": "\r\n \x3cdiv class=\"page-heading\"\x3e\x3ch1\x3eSEARCH
RESULTS 1 - 40 OF 200\x3c/h1\x3e\x3c/div\x3e\r\n\r\n
\x3cdiv class=\"right-content-list\"\x3e\r\n\r\n
\x3cdiv class=\"top-buttons-adm-lft\"\x3e\r\n
I am attempting to process this as a JSON Object (which is what this looks like) using Python's Requests library. 我正在尝试使用Python的Requests库将其作为JSON对象(看起来像这样)进行处理。
I have used the following methods to encode/process the text: 我使用以下方法来编码/处理文本:
unicodedata.normalize("NFKD", get_city_json.text).encode('utf-8', 'ignore')
unicodedata.normalize("NFKD", get_city_json.text).encode('ascii', 'ignore')
unicode(get_city_json.text)
However, even after repeated attempts, the text is rendered with the UTF-8 encoding and its characters. 但是,即使反复尝试,该文本仍使用UTF-8编码及其字符来呈现。 The Content-Type returned by the web app is
"text/javascript; charset=utf-8"
Web应用程序返回的Content-Type是
"text/javascript; charset=utf-8"
I want to be able to process it as a regular JSON/JavaScript Object for parsing and reading. 我希望能够将其作为常规的JSON / JavaScript对象进行解析和读取。
Help would be greatly appreciated! 帮助将不胜感激!
That isn't UTF-8
. 那不是
UTF-8
。 It HTML encoded text. 它是HTML编码的文本。
You can decode it using the following: 您可以使用以下方法对其进行解码:
import HTMLParser
html_parser = HTMLParser.HTMLParser()
unescaped = html_parser.unescape(json_value)
print unescaped
import html.parser
html_parser = html.parser.HTMLParser()
unescaped = html_parser.unescape(json_value)
print unescaped
If you unescape your string with these you should get 如果您用这些字符串解串,您应该得到
<div class="page-heading"><h1>SEARCH RESULTS 1 - 40 OF 200</h1></div>
<div class="right-content-list">
<div class="top-buttons-adm-lft">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.