0

I am just the beginner of python, so sorry about this basic problem

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)

I am using Jupiter to edit, but after import pymongo , I can't list the data I stored in MongoDB. What could be the reason?

 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({'title':title,'price':price,'date':date,'area':area})

       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)
shally
  • 1
  • 4
  • 1
    Please show your program formatted as code not as an image. – CaptSolo Apr 17 '16 at 20:47
  • Does your code work OK when run directly from Python (and not from Jupyter)? I suspect not. – CaptSolo Apr 17 '16 at 20:49
  • You need to add more information to your question. What is the name of the collection you are trying to query in Mongo? Right now it looks like you are just looping through a list of strings. Is `item_info3` a collection in Mongo? – markwatsonatx Apr 17 '16 at 21:58
  • I update my original code which can run directly from Python and can show the information I seized online... – shally Apr 18 '16 at 10:54
  • Did you see my answer? In your notebook item_info is being set to an array of strings - with a single string. My answer shows one way of retrieving data from the item_info3 collection. Not sure if that helps. – markwatsonatx Apr 18 '16 at 17:42

1 Answers1

0

It looks like you are looping through an array of strings and trying to call find on a string:

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

If item_info3 is the name of your mongo collection in the ceshi database then you should do something like:

item_info = ceshi['item_info3']

Even then, I don't believe your find query is correct. It should be something like this:

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

More information on Mongo/python:

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

markwatsonatx
  • 3,391
  • 2
  • 21
  • 19