您所在的位置: 首頁 > 裝修知識 >

基于python的租房數據分析「python爬取個人信息」

今天的Python教程講信息數據的獲取,這里首先收集趕集網和自如網的信息。

1. 趕集網信息獲取

I. 獲取當頁內容

這里的規則比較明顯,獲取網頁內容用xpath解析即可,各個板塊的信息都很容易獲取,最后用列表保存并返回即可,首先循環出每個divs塊,對里面的每個版塊內容逐個獲取

def get_this_page_gj(url, tmp): html = etree.HTML(requests.get(url).text) divs = html.xpath("http://div[@class="f-list-item ershoufang-list"]") for div in divs: title = div.xpath("./dl/dd[@class="dd-item title"]/a/text()")[0] house_url = div.xpath("./dl/dd[@class="dd-item title"]/a/@href")[0] size = "、".join(div.xpath("./dl/dd[@class="dd-item size"]/span/text()")) address = "-".join([ data.strip() for data in divs[0].xpath("./dl/dd[@class="dd-item address"][1]//a//text()") if data.strip() != "" ] ) agent_string = div.xpath("./dl/dd[@class="dd-item address"][2]/span/span/text()")[0] agent = re.sub(" ", "", agent_string) price = div.xpath("./dl/dd[@class="dd-item info"]/div[@class="price"]/span[@class="num"]/text()")[0] tmp.append([ title, size, price, address, agent, house_url ]) return tmp

II. URL構造

訪問首頁鏈接,獲取總頁數,按照url的訪問規則構造url,調用獲取當頁數據的方法即可,這里的url都是以http://cd.ganji.com/zufang/pn開頭的,后面跟上網頁的頁碼

def house_gj(headers): index_url = "http://cd.ganji.com/zufang/" html = etree.HTML(get_html(index_url, headers)) total = html.xpath("http://div[@class="pageBox"]/a[position() = last() -1]/span/text()")[0] result = [] for num in range(1, int(total) 1): result = get_this_page_gj("http://cd.ganji.com/zufang/pn{}".format(num), []) print("完成讀取第{}頁/趕集網".format(num)) return result

2 .

這里和趕集網類似,結構也相似,同樣的獲取方式,我們也抓取基礎信息加url鏈接,區別在于這里的價格可能不太好獲取,并不是直接顯示,而是以圖片 偏移量的形式展示

1. 價格獲取

每個數字對應一張圖片,圖片中的數字會根據style中設置的偏移去原圖中獲取,每頁的原圖也不盡相同,所以處理起來比較麻煩

這里我們仔細留心的會發現其實每個數字間的間距是一樣的,可以自己在頁面上更改數值查看規律,每個數字間的距離是21.4px,從原圖的左邊開始做偏移,根據偏移確定對應的數字,返回的數字下標 = |偏移量/21.4|,當然這里根據頁面圖片、內容等元素會有微小的誤差,但都是極小的誤差了,最后取個整去原圖的數字列表中取得對應下標的值即可,這里我們用到tesseract來對圖片進行解析

............price_strings = div.xpath("./div[@class="info-box"]/div[@class="price"]/span[@class="num"]/@style")offset_list = []for data in price_strings: offset_list.append(re.findall("position: (.*?)px", data)[0])style_string = html.xpath("http://div[@class="info-box"]/div[@class="price"]/span[@class="num"]/@style")[0]pic = "http:" re.findall(r"background-image: url((.*?));.*?", style_string)[0]price = get_price_zr(pic, offset_list)def get_price_zr(pic_url, offset_list): """ 這里的index保存所有數字的下標值,等待圖片解析完成獲取對應下標的價格數字 """ index, price = [], [] with open("pic.png", "wb") as f: f.write(requests.get(pic_url).content) code_list = list(pytesseract.image_to_string(Image.open("pic.png"))) for data in offset_list: index.append(int(math.fabs(eval(data)/21.4))) for data in index: price.append(code_list[data]) return "".join(price) pic_url是每頁的原圖地址,將之下載下來后用pytesseract解析,最后返回每個下標對應的數字所組成的新的數字字符串(價格),offset_list是獲取的每個數字的偏移值組成的列表

2. 自如網數據獲取

這里和趕集網類似,結構也相似,同樣的獲取方式,我們也抓取基礎信息加url鏈接,區別在于這里的價格可能不太好獲取,并不是直接顯示,而是以圖片 偏移量的形式展示

I. 價格獲取

每個數字對應一張圖片,圖片中的數字會根據style中設置的偏移去原圖中獲取,每頁的原圖也不盡相同,所以處理起來比較麻煩

這里我們仔細留心的會發現其實每個數字間的間距是一樣的,可以自己在頁面上更改數值查看規律,每個數字間的距離是21.4px,從原圖的左邊開始做偏移,根據偏移確定對應的數字,返回的數字下標 = |偏移量/21.4|,當然這里根據頁面圖片、內容等元素會有微小的誤差,但都是極小的誤差了,最后取個整去原圖的數字列表中取得對應下標的值即可,這里我們用到tesseract來對圖片進行解析

............price_strings = div.xpath("./div[@class="info-box"]/div[@class="price"]/span[@class="num"]/@style")offset_list = []for data in price_strings: offset_list.append(re.findall("position: (.*?)px", data)[0])style_string = html.xpath("http://div[@class="info-box"]/div[@class="price"]/span[@class="num"]/@style")[0]pic = "http:" re.findall(r"background-image: url((.*?));.*?", style_string)[0]price = get_price_zr(pic, offset_list)def get_price_zr(pic_url, offset_list): """ 這里的index保存所有數字的下標值,等待圖片解析完成獲取對應下標的價格數字 """ index, price = [], [] with open("pic.png", "wb") as f: f.write(requests.get(pic_url).content) code_list = list(pytesseract.image_to_string(Image.open("pic.png"))) for data in offset_list: index.append(int(math.fabs(eval(data)/21.4))) for data in index: price.append(code_list[data]) return "".join(price) pic_url是每頁的原圖地址,將之下載下來后用pytesseract解析,最后返回每個下標對應的數字所組成的新的數字字符串(價格),offset_list是獲取的每個數字的偏移值組成的列表

II. 獲取當頁數據

這里和趕集網類似,我們構造獲取每頁數據的函數,之后調用函數傳入每頁的url即可,這里可以關注一下xpath的擴展用法(contains函數)和正則獲取原圖鏈接

def get_this_page_zr(url, tmp): html = etree.HTML(requests.get(url).text) divs = html.xpath("http://div[@class="item"]") for div in divs: if div.xpath("./div[@class="info-box"]/h5/a/text()"): title = div.xpath("./div[@class="info-box"]/h5/a/text()")[0] else: continue link = "http:" div.xpath("./div[@class="info-box"]/h5/a/@href")[0] location = div.xpath("./div[@class="info-box"]/div[@class="desc"]/div[@class="location"]/text()")[0] area = div.xpath("./div[@class="info-box"]/div[@class="desc"]/div[contains(text(), "㎡")]/text()")[0] price_strings = div.xpath("./div[@class="info-box"]/div[@class="price"]/span[@class="num"]/@style") offset_list = [] for data in price_strings: offset_list.append(re.findall("position: (.*?)px", data)[0]) style_string = html.xpath("http://div[@class="info-box"]/div[@class="price"]/span[@class="num"]/@style")[0] pic = "http:" re.findall(r"background-image: url((.*?));.*?", style_string)[0] price = get_price_zr(pic, offset_list) tag = "、".join(div.xpath("./div[@class="info-box"]//div[@class="tag"]/span/text()")) tmp.append([ title, tag, price, area, location, link ]) return tmp

III. url構造

原理同趕集網的一樣,主要關注一下xpath的擴展用法position()=last()

def house_zr(headers): index_url = "http://cd.ziroom.com/z/" html = etree.HTML(get_html(index_url, headers)) total = html.xpath("http://div[@class="Z_pages"]/a[position()=last()-1]/text()")[0] result = [] for num in range(1, int(total) 1): result = get_this_page_zr("http://cd.ziroom.com/z/p{}/".format(num), []) print("完成讀取第{}頁/自如網".format(num)) return result鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如有侵權行為,請第一時間聯系我們修改或刪除,多謝。

免責聲明:本網站所有信息僅供參考,不做交易和服務的根據,如自行使用本網資料發生偏差,本站概不負責,亦不負任何法律責任。如有侵權行為,請第一時間聯系我們修改或刪除,多謝。