Python爬取鏈家二手房資料 重慶地區

2022-07-25 09:33:11 字數 2731 閱讀 3922

最近在學習資料分析的相關知識,打算找乙份資料做訓練,於是就打算用python爬取鏈家在重慶地區的二手房資料。

鏈家的頁面如下:

爬取**如下:

import

requests, json, time

from bs4 import

beautifulsoup

import

re, csv

defparse_one_page(url):

headers=

r = requests.get(url, headers=headers)

soup = beautifulsoup(r.text, '

lxml')

results = soup.find_all(class_="

clear logclickdata")

for item in

results:

output =

#從url中獲得區域

/')[-3])

#獲得戶型、面積、朝向等資訊,有無電梯的資訊可能會有缺失,資料清理可以很方便的處理

info1 = item.find('

div', '

houseinfo

').text.replace('

', '').split('|'

)

for t in

info1:

#獲得總價

div', '

totalprice

').text)

#獲得年份資訊,如果沒有就為空值

info2 = item.find('

div', '

positioninfo

').text.replace('

', ''

)

if info2.find('

年') != -1:

pos = info2.find('年'

)

else:'

')#獲得單價

div', '

unitprice

').text)

#print(output)

write_to_file(output)

defwrite_to_file(content):

#引數newline保證輸出到csv後沒有空行

with open('

data.csv

', '

a', newline=''

) as csvfile:

writer =csv.writer(csvfile)

#writer.writerow(['region', 'garden', 'layout', 'area', 'direction', 'renovation', 'elevator', 'price', 'year', 'perprice'])

writer.writerow(content)

defmain(offset):

regions = ['

jiangbei

', '

yubei

', '

nanan

', '

banan

', '

shapingba

', '

jiulongpo

', '

yuzhong

', '

dadukou

', '

jiangjing

', '

fuling',

'wanzhou

', '

hechuang

', '

bishan

', '

changshou1

', '

tongliang

', '

beibei']

for region in

regions:

for i in range(1, offset):

url = '

' + region + '

/pg'+ str(i) + '/'

html =parse_one_page(url)

time.sleep(1)

print('{} has been writen.'.format(region))

main(101)
鏈家**的資料最多隻顯示100頁,所以這裡我們爬取各個區域的前100頁資訊,有的可能沒有100頁,但並不影響,爬取結果如下(已經對資料做了一點處理,有問題的資料出現在有無電梯那一列和小區名那一列,只要排個序然後整體移動單元內容即可,年份缺失後面再做處理):

接下來,我們用excel的資料透視表簡單看一下資料的數量資訊:

從表中我們可以看到,此次共爬取了33225條資料,elevator這一項有很多資料缺失,year這一項由於在爬蟲時使用空格代替了空值,所以這一項也存在一些資料缺失。現在有了資料,後面就可以開始對這些資料進行分析了。

[1] 

Python爬取鏈家二手房資訊

2 資料庫表結構 使用物件導向的方式,搭建專案框架 import requests from bs4 import beautifulsoup import pymysql class lianjiaspider mydb pymysql.connect localhost root 123456 ...

python爬取鏈家二手房的資料

開啟鏈家官網,進入二手房頁面,選取某個城市,可以看到該城市 總數以及 列表資料。某些 的資料是存放在html中,而有些卻api介面,甚至有些加密在js中,還好鏈家的 資料是存放到html中 通過requests請求頁程式設計客棧面,獲取每頁的html資料 爬取的url,預設爬取的南京的鏈家房產資訊 ...

python爬蟲爬取鏈家二手房資訊

問題一 鏈家 也有反爬蟲策略和robots限制,robots限制忽略 不然沒法爬 另外頻繁爬取會直接導致被ban,需要隔天才會解禁止。防止被ban的方法有多種,1.禁止cookie 2.設定header 3.加大爬取間隔 4.使用 我只用了前三種方法,具體可以在settings.py 和middle...