python - 在python中将html页面转换为二进制格式

标签 python html mongodb binary

假设我有这两行:

c=urllib2.urlopen(myUrl)
html=c.read()

我是 Python 的新手,正在寻找一种方法来将存储在 html 中的 HTML 代码转换为二进制数组,这样我就可以轻松地存储在我的 MongoDB 中,而不必担心代码的编码问题我下载的网页。

建议?

最佳答案

pymongo 驱动程序已经有将字符串导入为二进制的方法。下面是这个例子:

import pymongo
import bson.binary

from pymonngo import MongoClient
from bson.binary import Binary

client = MongoClient()
db = client.test

db.btest.insert({ "bindata": Binary("Hello",0) })

db.btest.find_one()

这给了你:

{u'_id': ObjectId('5582b33c268e1505371a5477'), u'bindata': Binary('Hello', 0)}

或者从mongo shell:

> db.btest.findOne()
{
    "_id" : ObjectId("5582b33c268e1505371a5477"),
    "bindata" : BinData(0,"SGVsbG8=")
}

因此您的字符串现在已在您的文档中编码为 BSON 二进制类型。

请注意,它始终是编码字节而不是原始字节,因为 BSON 规范就是这样做的。

关于python - 在python中将html页面转换为二进制格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30914593/

相关文章:

html - IP 地址的正确输入类型

mongodb - 从本地主机到 cosmosDb 的 Mongorestore 失败, "disable retryable writes by specifying "retrywrites=false”

iphone - 如何在 iPad 上运行/移植 MongoDB?

python - 我的 PostgreSQL docker 镜像数据库在 Django 上不存在

python - BaseOperator.xcom_pull 中的上下文参数是什么

python - 在Windows上创建的.sh文件无法在Linux上运行

python - pydtmx 或 libdtmx 如何返回 FNC1 字符

css - margin-top 在内联 block 表单中无法正常工作

php - 如何在moodle主题设计中定义自定义 block 区域?

javascript - 如何查找与查询中具有完全相同数组条目的文档