怎么把python 用urllib2爬取的内容 存到数据库中
1个回答
展开全部
使用mongodb很简单。
首先安装pymongo:
pip install pymongo
代码实现:用urllib2读取数据,打包成JSON格式插入到mongodb中。
from pymongo import MongoClient
try:
from urllib2 importurlopen, Request, HTTPError, URLError
except ImportError:
from urllib.request import urlopen, Request, HTTPError, URLError
result = []
try:
f = urlopen('http://www.dynamsoft.com', timeout=3)
while 1:
tmp = f.read(10240)
if len(tmp) == 0:
break
else:
result.append(tmp)
f.close()
except HTTPError, URLError:
print URLError.code
content = ''.join(result)
post = {"author": "yushulx",
"content": content
}
client = MongoClient()
db = client.test_database
posts = db.posts
posts.insert(post)
print posts.find_one({"author": "yushulx"})
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询