【问题标题】:How do I remove double quotes from whithin retreived JSON data如何从检索 JSON 数据中删除双引号
【发布时间】:2020-03-05 15:38:37
【问题描述】:

我目前正在使用 BeautifulSoup 从工作网站上抓取列表,并通过网站的 HTML 代码将数据输出到 JSON 中。

我用正则表达式修复了出现的错误,但这个特殊问题让我陷入了困境。在网络抓取职位列表时,我没有从每个感兴趣的容器中提取信息,而是选择在 HTML 源代码 (< script type = "application/ld+json" >) 中提取 JSON 数据。从那里我将 BeautifulSoup 结果转换为字符串,清除 HTML 剩余部分,然后将字符串转换为 JSON。但是,由于职位列表中使用引号的文本,我遇到了障碍。由于实际数据很大,我就用一个替代品吧。

example_string = '{"Category_A" : "Words typed describing stuff",
                   "Category_B" : "Other words speaking more irrelevant stuff",
                   "Category_X" : "Here is where the "PROBLEM" lies"}'

现在上面的内容不会在 Python 中运行,但我从职位列表的 HTML 中提取的字符串几乎是上述格式。传入json.loads()时,返回错误:json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 5035

我完全不确定如何解决这个问题。

编辑 这是导致错误的实际代码:

from bs4 import BeautifulSoup
from urllib.request import urlopen
import json, re

uClient = urlopen("http://www.ethiojobs.net/display-job/227974/Program-Manager---Mental-Health%2C-Child-Care-Gender-%26-Protection.html")
page_html = uClient.read()
uClient.close()

listing_soup = BeautifulSoup(page_html, "lxml")

json_script = listing_soup.find("script", "type":"application/ld+json"}).strings

extracted_json_str = ''.join(json_script)

## Clean up the string with regex
extracted_json_str_CLEAN1 = re.sub(pattern = r"\r+|\n+|\t+|\\l+|  |&nbsp;|amp;|\u2013|</?.{,6}>", # last is to get rid of </p> and </strong>
                                repl='', 
                                string = extracted_json_str)
extracted_json_str_CLEAN2 = re.sub(pattern = r"\\u2019",
                                repl = r"'",
                                string = extracted_json_str_CLEAN1)
extracted_json_str_CLEAN3 = re.sub(pattern=r'\u25cf',
                                repl=r" -",
                                string = extracted_json_str_CLEAN2)
extracted_json_str_CLEAN4 = re.sub(pattern=r'\\',
                                repl="",
                                string = extracted_json_str_CLEAN3)

## Convert to JSON (HERE'S WHERE THE ERROR ARISES)
json_listing = json.loads(extracted_json_str_CLEAN4)

我确实知道导致错误的原因:在 job description 中目标 4 的最后一个要点中,作者在提到工作的必需任务(即“质量控制”)时使用了引号。我一直在从这些工作列表中提取信息的方式,一个使用引号的简单实例导致我的整个方法崩溃。当然必须有更好的方法来构建这个脚本,而不需要像这样的责任(以及必须使用正则表达式来修复出现的每个故障)。

谢谢!

【问题讨论】:

  • 没有安全的方法可以从适用于所有情况的 JSON 字符串中删除错误的引号。向我们展示您是如何获得该 JSON 的。问题的根源应该在那个代码中。
  • 您是如何构建 JSON 的?听起来您是手动操作而不是使用适当的序列化程序(例如json.dumps)。一个合适的序列化器不会犯这种错误。
  • 虽然你当时打电话给json.loads,所以不清楚为什么JSON会参与这个过程。正如您所描述的,原始网页的内容不是任何类型的 JSON,无论是有效还是无效,JSON 是您出于某种原因尝试自己构建的东西。

标签: python json python-3.x string double-quotes


【解决方案1】:

如果你想在你的值中使用双引号(“),你需要应用转义序列(\)。所以,你的字符串输入到 json.loads() 应该如下所示。

example_string = '{"Category_A": "Words typed describing stuff", "Category_B": "Other words speaking more irrelevant stuff", "Category_X": "Here is where the \\"PROBLEM\\" lies"}'

json.loads 可以解析这个。

【讨论】:

  • 解释/调查如何自动转义引号,因为 OP 所说的数据来自网页
【解决方案2】:
# WHen you extracting this I think you shood make a chekc for this.
# example:
if "\"" in extraction:
    extraction = extraction.replace("\"", "\'")
print(extraction)

在这种情况下,您将“从提取中转换为”,我的意思是您需要转换的东西,因为如果您想在字符串中使用“,则python为您提供了一种同时使用两者的方法,您需要反转该符号:

示例:

"this is a 'test'"
'this was a "test"'
"this is not a \"test\""
#in case the condition is meat
if "\"" in item:
    #use this
    item = item.replace("\"", "\'")
    #or use this
    item = item.replace("\"", "\\\"")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 2021-11-20
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多