Python手拉手教你爬取貝殼房源數(shù)據(jù)的實(shí)戰(zhàn)教程
一、爬蟲是什么?
在進(jìn)行大數(shù)據(jù)分析或者進(jìn)行數(shù)據(jù)挖掘的時(shí)候,數(shù)據(jù)源可以從某些提供數(shù)據(jù)統(tǒng)計(jì)的網(wǎng)站獲得,也可以從某些文獻(xiàn)或內(nèi)部資料中獲得,但是這些獲得數(shù)據(jù)的方式,有時(shí)很難滿足我們對(duì)數(shù)據(jù)的需求,而手動(dòng)從互聯(lián)網(wǎng)中去尋找這些數(shù)據(jù),則耗費(fèi)的精力過(guò)大。此時(shí)就可以利用爬蟲技術(shù),自動(dòng)地從互聯(lián)網(wǎng)中獲取我們感興趣的數(shù)據(jù)內(nèi)容,并將這些數(shù)據(jù)內(nèi)容爬取回來(lái),作為我們的數(shù)據(jù)源,從而進(jìn)行更深層次的數(shù)據(jù)分析,并獲得更多有價(jià)值的信息。 在使用爬蟲前首先要了解爬蟲所需的庫(kù)(requests)或者( urllib.request ),該庫(kù)是為了爬取數(shù)據(jù)任務(wù)而創(chuàng)建的。
二、使用步驟
1.引入庫(kù)
代碼如下(示例):
import os import urllib.request import random import time class BeikeSpider: def __init__(self, save_path="./beike"): """ 貝殼爬蟲構(gòu)造函數(shù) :param save_path: 網(wǎng)頁(yè)保存目錄 """
2.讀入數(shù)據(jù)
代碼如下 :
# 網(wǎng)址模式 self.url_mode = "http://{}.fang.ke.com/loupan/pg{}/" # 需爬取的城市 self.cities = ["cd", "sh", "bj"] # 每個(gè)城市爬取的頁(yè)數(shù) self.total_pages = 20 # 讓爬蟲程序隨機(jī)休眠5-10秒 self.sleep = (5, 10) # 網(wǎng)頁(yè)下載保存根目錄 self.save_path = save_path # 設(shè)置用戶代理,是爬蟲程序偽裝成瀏覽器 self.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36"} # 代理IP的信息 self.proxies = [ {"https": "123.163.67.50:8118"}, {"https": "58.56.149.198:53281"}, {"https": "14.115.186.161:8118"} ] # 創(chuàng)建保存目錄 if not os.path.exists(self.save_path): os.makedirs(self.save_path) def crawl(self): """ 執(zhí)行爬取任務(wù) :return: None """
該處使用的url網(wǎng)絡(luò)請(qǐng)求的數(shù)據(jù)。
3.隨機(jī)選擇一個(gè)ip地址構(gòu)建代理服務(wù)器
for city in self.cities: print("正在爬取的城市:", city) # 每個(gè)城市的網(wǎng)頁(yè)用單獨(dú)的目錄存放 path = os.path.join(self.save_path, city) if not os.path.exists(path): os.makedirs(path) for page in range(1, self.total_pages+1): # 構(gòu)建完整的url url = self.url_mode.format(city, page) # 構(gòu)建Request對(duì)象, 將url和請(qǐng)求頭放入對(duì)象中 request = urllib.request.Request(url, headers=self.headers) # 隨機(jī)選擇一個(gè)代理IP proxy = random.choice(self.proxies) # 構(gòu)建代理服務(wù)器處理器 proxy_handler = urllib.request.ProxyHandler(proxy) # 構(gòu)建opener opener = urllib.request.build_opener(proxy_handler) # 使用構(gòu)建的opener打開網(wǎng)頁(yè) response = opener.open(request) html = response.read().decode("utf-8") # 網(wǎng)頁(yè)保存文件名(包含路徑) filename = os.path.join(path, str(page)+".html") # 保存網(wǎng)頁(yè) self.save(html, filename) print("第%d頁(yè)保存成功!" % page) # 隨機(jī)休眠 sleep_time = random.randint(self.sleep[0], self.sleep[1]) time.sleep(sleep_time)
該處除隨機(jī)選擇ip地址以外還會(huì)限制爬取數(shù)據(jù)的速度,避免暴力爬取。
4.運(yùn)行代碼
def save(self, html, filename): """ 保存下載的網(wǎng)頁(yè) :param html: 網(wǎng)頁(yè)內(nèi)容 :param filename: 保存的文件名 :return: """ f = open(filename, 'w', encoding="utf-8") f.write(html) f.close() def parse(self): """ 解析網(wǎng)頁(yè)數(shù)據(jù) :return: """ pass if __name__ == "__main__": spider = BeikeSpider() spider.crawl()
運(yùn)行結(jié)果就會(huì)這樣,會(huì)保存在你的文件夾中。
總結(jié)
這里對(duì)文章進(jìn)行總結(jié):今天分析這波代碼目的是為了讓大家清晰明亮的了解python爬蟲的運(yùn)作,和大家一起學(xué)習(xí)
以上就是今天要講的內(nèi)容,本文僅僅簡(jiǎn)單介紹了pandas的使用,而urllib.request提供了大量能使我們快速便捷地爬取數(shù)據(jù)。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。