爬取北京地区的租房信息

import requests
from bs4 import BeautifulSoup, BeautifulStoneSoup
import time
header={'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
#爬取北京地区短租房信息
def judgement_sex(class_name):
    if class_name==['member_icol']:
        return '女'
    else:
        return '男'
def get_link(url):   
    data=requests.get(url,headers=header)
    soup=BeautifulSoup(data.text,'lxml')
    links=soup.select('#page_list > ul > li > a')
    for link in links:
        href=link.get("href")
        get_info(href)
def get_info(url):
    data=requests.get(url,headers=header)
    soup=BeautifulSoup(data.text,'lxml')
    
    titles=soup.select('div.pho_info > h4')
    addresses=soup.select('span.pr5')
    prices=soup.select('#pricePart > div.day_l > span')
    imgs=soup.select('#floatRightBox > div.js_box.clearfix > div.member_pic > a > img')
    names=soup.select('#floatRightBox > div.js_box.clearfix > div.w_240 > h6 > a')
    sexs=soup.select('#floatRightBox > div.js_box.clearfix > div.member_pic > div')
    
    for title,address,price,img,name,sex in zip(titles,addresses,prices,imgs,names,sexs):
        data={'title':title.get_text().strip(),
              'address':address.get_text().strip(),
              'price':price.get_text().strip(),
              'img':img.get('src'),
              'name':name.get_text(),
              'sex':judgement_sex(sex.get("class"))
              }
        print(data)
if __name__=='__main__':
    urls=['http://bj.xiaozhu.com/search-duanzufang-p{}-0/'.format(number) for number in range(1,14)]
    for single_url in urls:
        get_link(single_url)
        time.sleep(2)

猜你喜欢

转载自blog.csdn.net/qq_27584277/article/details/81332073