【问题标题】:Unable to return html content on REST API build with flask-restful无法在使用 flask-restful 的 REST API 构建上返回 html 内容
【发布时间】:2017-05-09 22:52:06
【问题描述】:
Absent<br>\r\n\t\t\tW - Withheld\r\n\t\t\t\r\n\t\t\t<b>Note:</b><br>\r\n\t\t\tThis Sheet is for general idea of marks you secured. This is not for official use. If any mistakes appear; record at NEB ledger will be referred.\r\n\t\t\r\n\r\n\t\t\r\n\t\t\r\n\r\n \t\t\r\n    </br></br></br></br></br></td>\n</tr>\n</table> is not JSON serializable

这是我在尝试访问传递所有值的句柄时遇到的确切错误。那里可以看到的 html 内容正是我需要的。我只复制了其中的几个部分,因为它是带有表格和行的大标签。我能够成功地将其加载到 Web 应用程序中,但在这里我正在使用 API。

不是 JSON 可序列化的

我正在抓取一个网站,并从该网站获取 html 表格内容并尝试通过我的 REST API 构建返回该内容,并使用 flask-restful

marks=requests.post("somesitelinkhere",data=data_load2)

soup2=BeautifulSoup(marks.content,'html.parser')
#this is the heading to show what Result it is
headinghere=soup2.find('h2').text
markstable=soup2.find('table')

return {'marksheet':markstable}

考虑上面的代码,我已经成功地抓取了内容,并且在 marktable 变量中可用。现在我如何返回它以便在尝试使用特定句柄访问时可以访问它。我必须做什么才能使其可序列化,或者在 REST API 中返回此类内容的最佳方式是什么。

【问题讨论】:

    标签: python json rest flask flask-restful


    【解决方案1】:

    markstable 是一个 BeautifulSoup.Tag 对象。您可以使用 markstable.contents 从中提取内容字符串并将其作为响应发送。

    【讨论】:

      猜你喜欢
      • 2014-12-15
      • 2015-05-14
      • 2018-10-29
      • 2020-04-01
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      • 2017-01-14
      • 1970-01-01
      相关资源
      最近更新 更多