美文网首页
python学习之旅-第一周week1-3

python学习之旅-第一周week1-3

作者: 张旭东0514 | 来源:发表于2016-05-19 10:46 被阅读0次

    week1-3作业
    学习python的第一周 5.17号完成练习week1-3 对小猪短租网的房东信息进行了初步爬去 爬去数量定位300+

    duanzu.png

    主要目标:抓取房屋信息中房主姓名,性别,价格,地址,图片等信息
    代码如下:

    from bs4 import BeautifulSoup
    import requests
    import time
    def get_message(url):
        wb_data = requests.get(url)
        soup = BeautifulSoup(wb_data.text,'lxml')
        titles = soup.select('body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > h4 > em ')#标题
        address2 = soup.select('body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > p > span.pr5')#地址
        prices = soup.select('#pricePart > div.day_l > span ')#价格
        landlords = soup.select('#floatRightBox > div.js_box.clearfix > div.member_pic > a > img ')#房东图片
        house_images = soup.select('#detailImageBox > div.pho_show_r > div > ul > li:nth-of-type(2) > img ')#房屋图片第一张
    
        landNames = soup.select('#floatRightBox > div.js_box.clearfix > div.w_240 > h6 > a ')#房东名字
        sexes = soup.select('div.member_pic > div ')
        def print_gender(class_name):
            if class_name[0] == 'member_ico1':
                return '女'
            if class_name[0] == 'member_ico':
                return '男'
        for title,address,price,landLoad,landName,sex,house_image in zip(titles,address2,prices,landlords,landNames,sexes,house_images):
            data = {
                '标题':title.get_text(),
                '地址':address.get_text().strip(),
                '价格':price.get_text(),
                '房东图片':landLoad.get('src'),
                '房东姓名':landName.get_text(),
                '性别':print_gender(sex.get('class')),
                '房屋图片':house_image.get('data-src'),
            }
            print(data)
    
    
    #获取当前页面里的跳转地址
    def get_pink_url(url):
        wb_data = requests.get(url)
        soup = BeautifulSoup(wb_data.text, 'lxml')
        links = soup.select('#page_list > ul > li > a ')
        for link in links:
            time.sleep(1)
            get_message(link.get('href'))
        return None
    #获取分页连接
    def get_pink_page():
        full_url = ['http://sh.xiaozhu.com/search-duanzufang-p{}-0/?startDate=2016-05-17&endDate=2016-05-18'.format(str(i)) for i in range(1,10,1)]
        for link in full_url:
            print(link)
            get_pink_url(link)
    #调用方法,进行爬取
    get_pink_page()
    

    爬去结果如下

    Paste_Image.png

    总结:
    -.通过本次week1-3练习,初步掌握了requests库用法
    -.本次练习难点在于房东性别的判断,本次通过一个方法里的if进行判定

    def print_gender(class_name):
            if class_name[0] == 'member_ico1':
                return '女'
            if class_name[0] == 'member_ico':
                return '男'
    

    相关文章

      网友评论

          本文标题:python学习之旅-第一周week1-3

          本文链接:https://www.haomeiwen.com/subject/rmfgrttx.html