python实战计划-对mongodb进行查询

目的:用这个网址http://www.jianshu.com/p/ef1028a4668e介绍的方法所爬取的内容放入mongodb中,并查询价格超过500的信息。
<pre><code>from bs4 import BeautifulSoupimport requestsimport pymongoclient = pymongo.MongoClient('localhost',27017)xiaozu = client['xiaozu']xinxi = xiaozu['xinxi']def sex_judge(sex): for isex in sex: if isex == 'member_ico': return 'male' elif isex == 'member_ico1': return 'female' else: return 'None'end_page = input('end_page:')for i in range(1, int(end_page)): base_url = 'http://bj.xiaozhu.com/search-duanzufang-p{}-0/'.format(i) #print(base_url) html1 = requests.get(base_url) bsObj1 = BeautifulSoup(html1.text, 'lxml') detail_url = bsObj1.find_all('a', {'class':'resule_img_a'}) for i in detail_url: html2 = requests.get(i.get('href')) bsObj2 = BeautifulSoup(html2.text, 'lxml') image = bsObj2.select('#curBigImage')[0].get('src') landlord_image = bsObj2.select('#floatRightBox > div.js_box.clearfix > div.member_pic > a > img')[0].get('src') title = bsObj2.select('body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > h4 > em')[0].get_text() address = list(bsObj2.select('body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > p > span')[0].stripped_strings) price = bsObj2.select('#pricePart > div.day_l > span')[0].get_text() name = bsObj2.select('#floatRightBox > div.js_box.clearfix > div.w_240 > h6 > a')[0].get_text() sexs = sex_judge(bsObj2.select('#floatRightBox > div.js_box.clearfix > div.member_pic > div')[0].get('class')) data = { 'image':image, 'landlord_image':landlord_image, 'title':title, 'address':address, 'price':int(price), 'name':name, 'sexs':sexs } xinxi.insert_one(data)for item in xinxi.find({'price':{'$gt':500}}): print(item)</code></pre>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,281评论 19 139
  • 最近自己写的简书文章太多了,导致很难找到之前写的文章。 于是,我想整理一篇目录,使用搜索快捷键:ctrl+F,即可...
    张云钱阅读 1,702评论 0 1
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,763评论 25 709
  • 前言:好像很久都没有像现在这样清醒的来想写些文字了,工作开始慢慢的步入了正规,不用再担心自己有什么不懂得了...
    方伊姗阅读 4,260评论 2 3
  • 你见过生命从眼前一点一点溜走的样子吗? 你见过鲜血从身体里一股一股涌出的样子吗? 如果这一切是你亲眼所见,如果你只...
    刘莫繁阅读 505评论 17 32