教你用python制作一個爬蟲軟體,城市二手房資訊一覽無余,,(附原始碼)
近年來隨著我國二手房市場的逐漸放開,進入市場的二手房數量不斷增加,二手房交易規模不斷擴大,市場規模也在不斷增長,資料顯示,截至2018年末,我國累計二手房交易金額已經超過30萬億元;2019年我國二手房市場規模6.7萬億元,預計到2021年將達到8.4萬億元,
眾所周知,發展二手房市場對于穩定住房價格,引導梯次消費,實作住房市場的健康發展具有重要的現實意義,但不可否認,二手房市場有效房源依舊供不應求,整體供求比例僅維持在1:4左右,如今,由于城市的擴張,新樓房通常只能建于遠離城市中心之地,交通便利性較差,遠不如建于城市中心地帶的二手房,
對于二手房資訊,我們也要多了解
軟體展示
輸入城市拼音的首字母,以及爬取的頁數即可
20210605_235436
開發工具
python
pycharm
tkinter庫(python內置庫)
parsel庫
爬蟲思路:
1.打開網址(采用鏈家網長沙二手房的網址)
https://cs.lianjia.com/ershoufang/pg1/
2.觀察url變化,點擊第二頁,第三頁
https://cs.lianjia.com/ershoufang/pg2/
https://cs.lianjia.com/ershoufang/pg3/
3.觀察網址是什么加載方式

可以確定為同步加載
4.打開北京鏈家二手房資訊,記錄url地址,觀察url地址的變化
https://bj.lianjia.com/ershoufang/
可以看出bj為北京首字母
5.開始寫爬蟲代碼
決議獲取文本內容 1、導包 2、變成CSS選擇器物件
由于獲取的內容都在ul標簽下的li標簽
變成CSS選擇器物件,下圖詳解

for page in range(1, pass_wd+1):
start_url = f'https://{user_name}.lianjia.com/ershoufang/pg{page}/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
}
response = requests.get(start_url, headers=headers).content.decode()
# pprint(response)
selector = parsel.Selector(response)
# 決議獲取文本內容 1、導包 2、變成CSS選擇器物件
lis = selector.css('.sellListContent li')
# self.text1.insert('insert', lis)
dit = {}
for li in lis:
title = li.css('.title a::text').get()
dit['標題'] = title
positionInfo = li.css('.positionInfo a::text').getall()
info = '-'.join(positionInfo)
dit['開發商'] = info
houseInfo = li.css('.houseInfo::text').get()
dit['房子資訊'] = houseInfo
followInfo = li.css('.followInfo::text').get()
dit['發布周期'] = followInfo
Price = li.css('.totalPrice span::text').get()
dit['售價/萬'] = Price
unitPrice = li.css('.unitPrice span::text').get()
dit['單價'] = unitPrice
定義可視化視窗,并設定視窗和主題大小布局
def __init__(self):
"""定義可視化視窗,并設定視窗和主題大小布局"""
self.window = tk.Tk()
self.window.title('城市二手房資料采集')
self.window.geometry('800x600')
"""創建label_user按鈕,與說明書"""
self.label_user = tk.Label(self.window, text='要爬取的城市首字母:', font=('Arial', 12), width=30, height=2)
self.label_user.pack()
"""創建label_user關聯輸入"""
self.entry_user = tk.Entry(self.window, show=None, font=('Arial', 14))
self.entry_user.pack(after=self.label_user)
"""創建label_passwd按鈕,與說明書"""
self.label_passwd = tk.Label(self.window, text="爬取多少頁:(小于100)", font=('Arial', 12), width=30, height=2)
self.label_passwd.pack()
"""創建label_passwd關聯輸入"""
self.entry_passwd = tk.Entry(self.window, show=None, font=('Arial', 14))
self.entry_passwd.pack(after=self.label_passwd)
"""創建Text富文本框,用于按鈕操作結果的展示"""
self.text1 = tk.Text(self.window, font=('Arial', 12), width=85, height=22)
self.text1.pack()
"""定義按鈕1,系結觸發事件方法"""
self.button_1 = tk.Button(self.window, text='爬取', font=('Arial', 12), width=10, height=1,
command=self.parse_hit_click_1)
self.button_1.pack(before=self.text1)
"""定義按鈕2,系結觸發事件方法"""
self.button_2 = tk.Button(self.window, text='清除', font=('Arial', 12), width=10, height=1, command=self.parse_hit_click_2)
self.button_2.pack(anchor="e")
代碼注釋很詳細,這里不過多介紹,
注意:定義按鈕,要系結觸發事件方法,command
創建視窗居中函式方法
def center(self):
"""創建視窗居中函式方法"""
ws = self.window.winfo_screenwidth()
hs = self.window.winfo_screenheight()
x = int((ws / 2) - (800 / 2))
y = int((hs / 2) - (600 / 2))
self.window.geometry('{}x{}+{}+{}'.format(800, 600, x, y))
定義觸發事件2,洗掉文本框中內容
def parse_hit_click_2(self):
"""定義觸發事件2,洗掉文本框中內容"""
self.entry_user.delete(0, "end")
self.entry_passwd.delete(0, "end")
self.text1.delete("1.0", "end")
原始碼展示:
import tkinter as tk
import requests, parsel
from pprint import pprint
class TKSpider(object):
def __init__(self):
"""定義可視化視窗,并設定視窗和主題大小布局"""
self.window = tk.Tk()
self.window.title('城市二手房資料采集')
self.window.geometry('800x600')
"""創建label_user按鈕,與說明書"""
self.label_user = tk.Label(self.window, text='要爬取的城市首字母:', font=('Arial', 12), width=30, height=2)
self.label_user.pack()
"""創建label_user關聯輸入"""
self.entry_user = tk.Entry(self.window, show=None, font=('Arial', 14))
self.entry_user.pack(after=self.label_user)
"""創建label_passwd按鈕,與說明書"""
self.label_passwd = tk.Label(self.window, text="爬取多少頁:(小于100)", font=('Arial', 12), width=30, height=2)
self.label_passwd.pack()
"""創建label_passwd關聯輸入"""
self.entry_passwd = tk.Entry(self.window, show=None, font=('Arial', 14))
self.entry_passwd.pack(after=self.label_passwd)
"""創建Text富文本框,用于按鈕操作結果的展示"""
self.text1 = tk.Text(self.window, font=('Arial', 12), width=85, height=22)
self.text1.pack()
"""定義按鈕1,系結觸發事件方法"""
"""即登錄按鈕,當點擊時將執行parse_hit_click_1方法,在真實使用場景中"""
"""parse_hit_click_1中可替換為自己寫的真正登錄函式,這里僅為示例"""
self.button_1 = tk.Button(self.window, text='爬取', font=('Arial', 12), width=10, height=1,
command=self.parse_hit_click_1)
self.button_1.pack(before=self.text1)
"""定義按鈕2,系結觸發事件方法"""
self.button_2 = tk.Button(self.window, text='清除', font=('Arial', 12), width=10, height=1, command=self.parse_hit_click_2)
self.button_2.pack(anchor="e")
def parse_hit_click_1(self):
"""定義觸發事件1,呼叫main函式"""
user_name = self.entry_user.get()
pass_wd = int(self.entry_passwd.get())
self.main(user_name, pass_wd)
def main(self, user_name, pass_wd):
"""爬蟲函式"""
# 抓取每頁的url
for page in range(1, pass_wd+1):
start_url = f'https://{user_name}.lianjia.com/ershoufang/pg{page}/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
}
response = requests.get(start_url, headers=headers).content.decode()
# pprint(response)
selector = parsel.Selector(response)
# 決議獲取文本內容 1、導包 2、變成CSS選擇器物件
lis = selector.css('.sellListContent li')
# self.text1.insert('insert', lis)
dit = {}
for li in lis:
title = li.css('.title a::text').get()
dit['標題'] = title
positionInfo = li.css('.positionInfo a::text').getall()
info = '-'.join(positionInfo)
dit['開發商'] = info
houseInfo = li.css('.houseInfo::text').get()
dit['房子資訊'] = houseInfo
followInfo = li.css('.followInfo::text').get()
dit['發布周期'] = followInfo
Price = li.css('.totalPrice span::text').get()
dit['售價/萬'] = Price
unitPrice = li.css('.unitPrice span::text').get()
dit['單價'] = unitPrice
"""爬取的內容展示在文本框中"""
self.text1.insert("insert", dit)
self.text1.insert("insert", '\n ') # 添加換行,好觀察一點
self.text1.insert("insert", '\n ')
printinfo = print(f'*********第{page}頁列印完成*********')
def parse_hit_click_2(self):
"""定義觸發事件2,洗掉文本框中內容"""
self.entry_user.delete(0, "end")
self.entry_passwd.delete(0, "end")
self.text1.delete("1.0", "end")
def center(self):
"""創建視窗居中函式方法"""
ws = self.window.winfo_screenwidth()
hs = self.window.winfo_screenheight()
x = int((ws / 2) - (800 / 2))
y = int((hs / 2) - (600 / 2))
self.window.geometry('{}x{}+{}+{}'.format(800, 600, x, y))
def run_loop(self):
"""禁止修改表單大小規格"""
self.window.resizable(False, False)
"""視窗居中"""
self.center()
"""視窗維持--持久化"""
self.window.mainloop()
if __name__ == '__main__':
t = TKSpider()
t.run_loop()
代碼寫完之后打包一下
命令列輸入pyinstaller -F 檔案.py

打包好后

此處代碼還不夠完善,希望各位能夠一起討論,指點迷津,,
原創不易,希望各位能夠一鍵三連,小弟在此謝謝了,
快去點贊收藏吧各位
祝大家學習python順利!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/286322.html
標籤:python
