美文网首页
Python 1-3

Python 1-3

作者: 木曜 | 来源:发表于2016-06-24 11:08 被阅读0次

    注意事项

    • Beautiful Soup 反馈结果的类型
    • strip() 函数如何去空格、回车等待
    • str.format()函数 格式化字符串
    from bs4 import BeautifulSoup
    import requests
    
    # 创建函数获取性别
    # 通过host_sex.get("class")返回的是列表,判断条件需要 "==列表"
    def get_host_sex(class_name):
        if class_name == ['member_girl_ico']:
            return '女'
        else:
            return '男'
    
    def get_urls_list(url):
        web_data = requests.get(url)
        soup = BeautifulSoup(web_data.text, 'lxml')
        links = soup.select('#page_list > ul > li > a')
        for link in links:
            get_item(link.get('href'))
    
    def get_item(url):
        web_data = requests.get(url)
        soup = BeautifulSoup(web_data.text, 'lxml')
    
        title = soup.select('div.pho_info > h4 > em')
        address = soup.select('div.pho_info > p > span.pr5')
        price = soup.select('div.day_l > span')
        img = soup.select('div.pho_show_l > div > div:nth-of-type(2) > img')
        host_name = soup.select('div.w_240 > h6 > a')
        host_img = soup.select('div.member_pic > a > img')
        host_sex = soup.select('div.w_240 > h6 > span')
    
        for title, address, price, img, host_name, host_img, host_sex in zip(title, address, price, img, host_name, host_img, host_sex ):
            data = {
                'title':title.get_text(),
                # 地址结果中存在空格和换行,通过strip()函数去除
                'address':address.get_text().strip(),
                'price':price.get_text(),
                'img':img.get("src"),
                'host_name':host_name.get_text(),
                'host_img':host_img.get("src"),
                'host_sex':get_host_sex(host_sex.get("class"))
            }
            print(data)
    
    # 获取url列表,其中页面数通过格式化字符串的函数str.format()获取
    page_urls = ['http://hz.xiaozhu.com/search-duanzufang-p{}-0/'.format(str(i)) for i in range(1,2)]
    for page_url in page_urls:
        get_urls_list(page_url)
    
    
    

    相关文章

      网友评论

          本文标题:Python 1-3

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