我正在使用jupyter进行编辑,但是在导入pymongo之后,为什么不能列出我存储在MongoDB中的数据?

时间:2016-04-17 20:44:22

标签: jupyter-notebook pymongo-3.x

我只是python的初学者,对这个基本问题感到抱歉

enter image description here

import pymongo

client = pymongo.MongoClient('localhost', 27017)
ceshi = client['ceshi']
item_info = ['item_info3']

for i in item_info:
    if i.find('area'):
        print(i)

我正在使用Jupiter进行编辑,但在导入pymongo之后,我无法列出我存储在MongoDB中的数据。可能是什么原因?

 from bs4 import BeautifulSoup
 import requests
 import time
 import pymongo


 client = pymongo.MongoClient('localhost',27017)
 ceshi = client['ceshi']
 url_list = ceshi['url_list3']
 item_info = ceshi['item_info3']

 def get_links_from(channel,pages,who_sells=0):
   #http://bj.58.com/diannao/0/pn2/
   list_view = '{}{}/pn{}/'.format(channel,str(who_sells),str(pages))
   wb_date = requests.get(list_view)
   time.sleep(1)
   soup = BeautifulSoup(wb_date.text,'lxml')
   links = soup.select('tr.zzinfo > td.t > a.t')
   if soup.find('td','t'):
       for link in links:
           item_link = link.get('href').split('?')[0]
       else:
           pass

       url_list.insert_one({'url':item_link})
       print(url_list)


 def get_item_info(url):
    wb_data = requests.get(url)
    soup = BeautifulSoup(wb_data.text,'lxml')
    no_longer_exist = '404' in 
    soup.find('script',type="text/javascript").get('src').split('/')
    if no_longer_exist:
        pass
    else:

        title = soup.title.text
        price = soup.select('span.price.c_f50')[0].text
        date = soup.select('li.time')[0].text
        area = list(soup.select('spam.c_25d a')[0].stripped_strings) if   
        soup.find_all('c_25d') else None                     

item_info.insert_one({'标题':标题,'价格':价格,'日期':日期,'区域&#39 ;:面积})

       print(item_info)


    #print({'title':title,'price':price,'date':date,'area':area})
get_item_info('http://bj.58.com/pingbandiannao/25347275157966x.shtml')
#get_links_from('http://bj.58.com/yunfuyongpin/',2)

1 个答案:

答案 0 :(得分:0)

看起来你正在遍历一个字符串数组并尝试在字符串上调用find:

item_info = ['item_info3']
for i in item_info:
    if i.find('area')

如果item_info3ceshi数据库中mongo集合的名称,那么您应该执行以下操作:

item_info = ceshi['item_info3']

即便如此,我也不相信你的查询是正确的。它应该是这样的:

for i in item_info.find():
    print(i)

有关Mongo / python的更多信息:

https://docs.mongodb.org/getting-started/python/query/