如何使用Beauty soup将爬行数据上传到python中的AZURE BLOB存储中?
我从一个URL抓取数据,并使用BeautifulSoup进行抓取。我想将已爬网的数据作为BLOB存储到AZURE BLOB存储中。下面是我在本地保存数据时的代码,这与我想直接上传到Azure时执行的操作相同如何使用Beauty soup将爬行数据上传到python中的AZURE BLOB存储中?,python,azure,beautifulsoup,azure-storage,azure-blob-storage,Python,Azure,Beautifulsoup,Azure Storage,Azure Blob Storage,我从一个URL抓取数据,并使用BeautifulSoup进行抓取。我想将已爬网的数据作为BLOB存储到AZURE BLOB存储中。下面是我在本地保存数据时的代码,这与我想直接上传到Azure时执行的操作相同 soup = BeautifulSoup(urlopen('www.abc.html')) outfile = open('C:\\Users\\ADMIN\\filename.txt','w') data = soup.encode("ascii","ignore") outfile
soup = BeautifulSoup(urlopen('www.abc.html'))
outfile = open('C:\\Users\\ADMIN\\filename.txt','w')
data = soup.encode("ascii","ignore")
outfile.write(data)
outfile.close
此代码已成功将网站数据保存在我的本地文件夹中,请帮助我将同一网站的数据直接保存在azure blob存储中。我在AZURE BLOB存储中有密钥和帐户
soup=BeautifulSoup(urlopen('www.abc.html'))
data = soup.encode("ascii","ignore")
block_blob_service.create_blob_from_text('containername', 'filename.txt', data)
我正在尝试上面的代码,但它不工作 没有任何信息显示什么版本的
BeautifulSoup
,方法urlopen
来自Python 2中的urlib
或urlib2
或urlib3
。根据您的代码,根据我的经验,我认为您使用的是BeautifulSoup4
和urlib2
,我试图重现您关于数据类型不是str
的问题,但由于我下面的代码有效,所以失败了
这是我的示例代码
from bs4 import BeautifulSoup
import urllib2
soup = BeautifulSoup(urllib2.urlopen("http://bing.com"))
data = soup.encode("ascii","ignore")
print type(data) # It's <type 'str'> here
from azure.storage.blob.blockblobservice import BlockBlobService
block_blob_service = BlockBlobService(account_name='<your-account-name>', account_key='<your-account-key>')
block_blob_service.create_container('mycontainer')
block_blob_service.create_blob_from_text('mycontainer1', 'filename.txt', data)
它对我也有用
希望能有所帮助。请编辑您的问题,并提供有关哪些问题不起作用的更多详细信息。是否有错误?@GauravMantri在我的blob代码中,参数“data”不是文本类型,create_blob_from_text()expect text参数。我无法找到其他方法。将“数据”转换为文本时出错,因为它是str类型。能否将此数据转换为字节数组?
from StringIO import StringIO
block_blob_service.create_blob_from_stream('mycontainer', 'filename2.txt', StringIO(data))