Python實(shí)戰(zhàn)計(jì)劃學(xué)習(xí)筆記1.3:爬取小豬短租的租房信息

爬取小豬短租上300條租房信息,包括標(biāo)題、地址、日租金、房東、房東性別、房東頭像、房屋圖片等,如下圖所示:


1.png

最終程序運(yùn)行截圖:

2.png

完整代碼:

from bs4 import BeautifulSoup
import requests
import time


def get_detail_info(url, data=None):

    # 爬取單條租房信息(標(biāo)題,圖片,房東,日租金,房東性別,房東頭像)
    wb_data = requests.get(url)
    soup = BeautifulSoup(wb_data.text, 'lxml')
    time.sleep(2)

    title = soup.select('h4 > em')[0].get_text()
    address = soup.select('span.pr5')[0].get_text()
    rent = soup.select('div.day_l > span')[0].get_text()
    image = soup.select('#curBigImage')[0].get('src')
    lorder_pic = soup.select('div.member_pic > a > img')[0].get('src')
    lorder_name = soup.select('a.lorder_name')[0].get_text()
    lorder_sex = soup.select('#floatRightBox > div.js_box.clearfix > div.w_240 > h6 > span')[0].get('class')

    def get_gender(class_name):
        if class_name == "member_boy_ico":
            return  "男"
        else:
            return "女"

    data = {
        '標(biāo)題': title,
        '地址': address,
        '日租金': rent,
        '圖片': image,
        '房東頭像': lorder_pic,
        '房東姓名': lorder_name,
        '房東性別': get_gender(lorder_sex)
    }

    print(data)


def get_links(url):
    # 獲取詳情頁(yè)鏈接
    wb_data = requests.get(url)
    soup = BeautifulSoup(wb_data.text, 'lxml')
    links = soup.select('#page_list > ul > li > a')
    for link in links:
        href = link.get('href')
        get_detail_info(href)


urls = ['http://su.xiaozhu.com/search-duanzufang-p{}-0/'.format(str(i) for i in range(1,10))]
# 獲取9頁(yè)的租房信息
for url in urls:
    get_links(url)

總結(jié):

用requests庫(kù)打開(kāi)網(wǎng)頁(yè)(get, post等方法)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容