
怎么把python 用urllib2爬取的内容 存到数据库中
展开全部
使用mongodb很简单。
首先安装pymongo:
1pip install pymongo
代码实现:用urllib2读取数据,打包成JSON格式插入到mongodb中。
1234567891011121314151617181920212223242526from pymongo import MongoClienttry: from urllib2 importurlopen, Request, HTTPError, URLErrorexcept ImportError: from urllib.request import urlopen, Request, HTTPError, URLErrorresult = []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.codecontent = ''.join(result)post = {"author": "yushulx","content": content}client = MongoClient()db = client.test_databaseposts = db.postsposts.insert(post)print posts.find_one({"author": "yushulx"})
首先安装pymongo:
1pip install pymongo
代码实现:用urllib2读取数据,打包成JSON格式插入到mongodb中。
1234567891011121314151617181920212223242526from pymongo import MongoClienttry: from urllib2 importurlopen, Request, HTTPError, URLErrorexcept ImportError: from urllib.request import urlopen, Request, HTTPError, URLErrorresult = []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.codecontent = ''.join(result)post = {"author": "yushulx","content": content}client = MongoClient()db = client.test_databaseposts = db.postsposts.insert(post)print posts.find_one({"author": "yushulx"})
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询