python实战计划2_1

整个编程过程没什么问题。。基本上20分钟搞定
就是直接存数据进mongodb会将价格存成string类型,没法用$gte来比对
用了int()强制转换。

import pymongo
import requests
from bs4 import BeautifulSoup



def get_information(url):#这个函数完成了爬取其中一个页面商品信息的任务
    # url = 'http://bj.xiaozhu.com/fangzi/1508951935.html'
    wb_data = requests.get(url)
    soup = BeautifulSoup(wb_data.text,'lxml')


    title = soup.select('div.pho_info > h4')[0].text
    address = soup.select('div.pho_info > p')[0].get('title')#[0]代表取数组的第1个元素,因为soup.select返回的是一个数组,然后获取title属性
    price = soup.select('div.day_l > span')[0].text
    pic = soup.select('#curBigImage')[0].get('src')
    fangdong_name = soup.select('#floatRightBox > div.js_box.clearfix > div.w_240 > h6 > a.lorder_name')[0].text
    fangdong_gender = soup.select('#floatRightBox > div.js_box.clearfix > div.member_pic > div')[0].get('class')[0]

    gender="男"
    if fangdong_gender=='member_ico':
        gender="男"
    elif fangdong_gender=='member_icol':
        gender="女"


    data={
        'title':title,
        'address':address,
        'price':price,
        'img':pic,
        'host_name':fangdong_name,
        'host_gender':gender
    }

    return data

def get_info():
    for i in range(1,4):
        oriurl="http://bj.xiaozhu.com/search-duanzufang-p"+str(i)+"-0/"
        # oriurl="http://bj.xiaozhu.com/"
        wb_data_ori = requests.get(oriurl)
        soup_ori = BeautifulSoup(wb_data_ori.text,'lxml')
        links=soup_ori.select('#page_list > ul > li > a')
        infos=[]
        for link in links :
            link=link.get('href')
            infos.append(get_information(link))
        return infos




client=pymongo.MongoClient('localhost',27017)
walden=client['walden']
sheet_duanzufang=walden['sheet_duanzufang']

infos=get_info()
for info in infos:
    print(info)
    sheet_duanzufang.insert_one(info)


for item in sheet_duanzufang.find():
    if int(item['price'])>=500:
        print(item)

结果也很简单

{'host_gender': '男', 'img': 'http://image.xiaozhustatic1.com/00,800,533/3,0,37,4642,1800,1200,7b756ae5.jpg', 'title': '\n东大桥地铁口,毗邻三里屯工体朝阳门世贸天阶\n', 'price': '598', 'host_name': 'himhimhuang', '_id': ObjectId('574307097ca76743808cace7'), 'address': '北京市朝阳区呼家楼街道'}
{'host_gender': '男', 'img': 'http://image.xiaozhustatic1.com/00,800,533/3,0,79,3464,1800,1200,937686ec.jpg', 'title': '\n长安街沿线 鸟瞰国贸 紧邻地铁温馨舒适大三居\n', 'price': '680', 'host_name': '好心情123', '_id': ObjectId('574307097ca76743808cace8'), 'address': '北京市朝阳区八里庄西里1号'}
{'host_gender': '男', 'img': 'http://image.xiaozhustatic1.com/00,800,533/3,0,24,2692,1800,1200,4a3c697f.jpg', 'title': '\n到鸟巢去散步,安慧东里敞亮大三居\n', 'price': '698', 'host_name': 'ocheese', '_id': ObjectId('574307097ca76743808caceb'), 'address': '北京市朝阳区新都市计划大厦'}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. 练习准备: MongoDB的准备:MongoDB学习—(1)安装时出现The default storag...
    linacqu阅读 2,905评论 0 0
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 32,997评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,064评论 19 139
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,357评论 0 33
  • (2016-04-29-Fri 16:04:21)
    菜五阅读 1,598评论 0 0

友情链接更多精彩内容