日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何高效地爬取链家的房源信息(四)

發(fā)布時(shí)間:2023/11/27 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何高效地爬取链家的房源信息(四) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Python實(shí)現(xiàn)的鏈家網(wǎng)站的爬蟲第四部分,最后一部分。


本系列文將以鏈家南京站為例,使用Python實(shí)現(xiàn)鏈家二手房源信息的爬蟲,將數(shù)據(jù)爬取,并存入數(shù)據(jù)庫中,以便使用。


本系列第一部分為基礎(chǔ):

如何高效地爬取鏈家的房源信息(一)


本系列第二部分為爬取小區(qū)信息:

如何高效地爬取鏈家的房源信息(二)


本系列第三部分為爬取在售二手房信息:

如何高效地爬取鏈家的房源信息(三)


本文是第四部分,爬取歷史成交二手房信息并存入數(shù)據(jù)庫,部分代碼依賴于第一部分,同時(shí)依賴于第二部分的結(jié)果。


在前文中已經(jīng)獲取了小區(qū)信息,并存在了數(shù)據(jù)庫中,直接讀庫遍歷小區(qū)進(jìn)行爬取:

def do_xiaoqu_chengjiao_spider(db_xq,db_cj):

? ? """

? ? 批量爬取小區(qū)成交記錄

? ? """

? ? count=0

? ? xq_list=db_xq.fetchall()

? ? for xq in xq_list:

? ? ? ? xiaoqu_chengjiao_spider(db_cj,xq[0],xq[1])

? ? ? ? count+=1

? ? ? ? print ('have spidered %d xiaoqu %s' % (count,xq[0]))

? ? print( 'done')


對(duì)某一個(gè)小區(qū)內(nèi)的所有成交房源進(jìn)行爬取,需要分頁:

def xiaoqu_chengjiao_spider(db_cj, xq_url=u"https://nj.lianjia.com/xiaoqu/1411000000391/", xq_name=u"default"):

? ? """

? ? 爬取小區(qū)成交記錄

? ? """

? ? url = xq_url.replace('xiaoqu/','chengjiao/c');

? ? try:

? ? ? ? req = urllib.request.Request(url, headers=hds[random.randint(0, len(hds) - 1)])

? ? ? ? source_code = urllib.request.urlopen(req, timeout=10).read()

? ? ? ? plain_text = source_code.decode('utf-8');

? ? ? ? soup = BeautifulSoup(plain_text,"html.parser")

? ? except (urllib.request.HTTPError, urllib.request.URLError) as e:

? ? ? ? print(e)

? ? ? ? exception_write('xiaoqu_chengjiao_spider', xq_url)

? ? ? ? return

? ? except Exception as e:

? ? ? ? print(e)

? ? ? ? exception_write('xiaoqu_chengjiao_spider', xq_url)

? ? ? ? return

? ? content = soup.find('div', {'class': 'page-box house-lst-page-box'})

? ? total_pages = 0

? ? if content:

? ? ? ? d = "d=" + content.get('page-data')

? ? ? ? loc = {}

? ? ? ? glb = {}

? ? ? ? exec(d, glb, loc);

? ? ? ? total_pages = loc['d']['totalPage']


? ? print(u"xiaoqu %s chengjiao totalpage %d" % (xq_name,total_pages));

? ? threads = []

? ? for i in range(total_pages):

? ? ? ? tmp= u'chengjiao/pg%dc'% (i + 1)

? ? ? ? url_page = url.replace('chengjiao/c',tmp);

? ? ? ? t = threading.Thread(target=chengjiao_spider, args=(db_cj, url_page))

? ? ? ? threads.append(t)

? ? for t in threads:

? ? ? ? t.start()

? ? for t in threads:

? ? ? ? t.join()


爬取單個(gè)頁面內(nèi)的成交記錄信息:

def chengjiao_spider(db_cj, url_page=u"https://nj.lianjia.com/chengjiao/pg4c1411000000142/"):

? ? """

? ? 爬取頁面鏈接中的成交記錄

? ? """

? ? print(u"爬取頁面%s成交記錄" % url_page);

? ? try:

? ? ? ? req = urllib.request.Request(url_page, headers=hds[random.randint(0, len(hds) - 1)])

? ? ? ? source_code = urllib.request.urlopen(req, timeout=10).read()

? ? ? ? plain_text = source_code.decode('utf-8');

? ? ? ? soup = BeautifulSoup(plain_text,"html.parser")

? ? except (urllib.request.HTTPError, urllib.request.URLError) as e:

? ? ? ? print(e)

? ? ? ? exception_write('chengjiao_spider', url_page)

? ? ? ? return

? ? except Exception as e:

? ? ? ? print(e)

? ? ? ? exception_write('chengjiao_spider', url_page)

? ? ? ? return


? ? recodenum = 0;

? ? cjs = soup.find('ul', {'class': 'listContent'});

? ? cj_list = cjs.findAll('li', {})

? ? for cj in cj_list:

? ? ? ? info_dict = {}

? ? ? ? title = cj.find('div', {'class': 'title'});

? ? ? ? houseInfo = cj.find('div', {'class': 'houseInfo'});

? ? ? ? dealDate = cj.find('div', {'class': 'dealDate'});

? ? ? ? totalPrice = cj.find('div', {'class': 'totalPrice'});

? ? ? ? positionInfo = cj.find('div', {'class': 'positionInfo'});

? ? ? ? source = cj.find('div', {'class': 'source'});

? ? ? ? unitPrice = cj.find('div', {'class': 'unitPrice'});

? ? ? ? dealHouseInfo = cj.find('div', {'class': 'dealHouseInfo'});

? ? ? ? dealCycleeInfo = cj.find('div', {'class': 'dealCycleeInfo'});


? ? ? ? href = title.find('a')

? ? ? ? if not href:

? ? ? ? ? ? continue

? ? ? ? info_dict.update({u'鏈接': href.attrs['href']})

? ? ? ? content = title.text.split()

? ? ? ? if content:

? ? ? ? ? ? info_dict.update({u'小區(qū)名稱': content[0]})

? ? ? ? ? ? info_dict.update({u'戶型': content[1]})

? ? ? ? ? ? info_dict.update({u'面積': content[2]})


? ? ? ? content = houseInfo.text.split('|') #unicode(cj.find('div', {'class': 'con'}).renderContents().strip())

? ? ? ? if content:

? ? ? ? ? ? info_dict.update({u'朝向': content[0].strip()})

? ? ? ? ? ? if len(content) >= 2:

? ? ? ? ? ? ? ? info_dict.update({u'裝修': content[1].strip()})

? ? ? ? ? ? if len(content) >= 3:

? ? ? ? ? ? ? ? info_dict.update({u'電梯': content[2].strip()})


? ? ? ? info_dict.update({u'簽約時(shí)間': dealDate.text})

? ? ? ? info_dict.update({u'簽約總價(jià)': totalPrice.text}) #注意值

? ? ? ? content = positionInfo.text.split()

? ? ? ? if len(content) >= 2:

? ? ? ? ? ? info_dict.update({u'樓層': content[0].strip()})?

? ? ? ? ? ? info_dict.update({u'年代樓型': content[1].strip()})??

? ? ? ? else:

? ? ? ? ? ? info_dict.update({u'樓層': content[0].strip()})??


? ? ? ? info_dict.update({u'來源': source.text})

? ? ? ? info_dict.update({u'簽約單價(jià)': unitPrice.text}) #可能為*

? ? ? ? #content = dealHouseInfo.text.split()

? ? ? ? if dealHouseInfo != None:

? ? ? ? ? ? for span in dealHouseInfo.find('span', {'class': 'dealHouseTxt'}).findAll('span'):

? ? ? ? ? ? ? ? if span.text.find(u'房屋') != -1:

? ? ? ? ? ? ? ? ? ? info_dict.update({u'稅費(fèi)': span.text})? # 滿幾年

? ? ? ? ? ? ? ? elif span.text.find(u'距') != -1:

? ? ? ? ? ? ? ? ? ? info_dict.update({u'地鐵': span.text})


? ? ? ? #content = dealCycleeInfo.text.split()

? ? ? ? if dealCycleeInfo != None:

? ? ? ? ? ? for span in dealCycleeInfo.find('span',{'class': 'dealCycleTxt'}).findAll('span'):

? ? ? ? ? ? ? ? if span.text.find(u'掛牌') != -1:

? ? ? ? ? ? ? ? ? ? info_dict.update({u'掛牌價(jià)': span.text})

? ? ? ? ? ? ? ? elif span.text.find(u'成交周期') != -1:

? ? ? ? ? ? ? ? ? ? info_dict.update({u'成交周期': span.text})


? ? ? ? command = gen_chengjiao_insert_command(info_dict)


? ? ? ? db_cj.execute(command, 1)

? ? ? ? recodenum += 1;

? ? print(u"爬取頁面%s成交記錄%d條" % (url_page,recodenum));



爬取的成交記錄信息將被存儲(chǔ)到數(shù)據(jù)庫表中。




整個(gè)爬取過程還是很快的,在爬取完小區(qū)、在售、成交三類信息之后,就可以拿這些數(shù)據(jù)去進(jìn)行想要做的分析了。


長(zhǎng)按進(jìn)行關(guān)注。


總結(jié)

以上是生活随笔為你收集整理的如何高效地爬取链家的房源信息(四)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

歡迎分享!

轉(zhuǎn)載請(qǐng)說明來源于"生活随笔",并保留原作者的名字。

本文地址:如何高效地爬取链家的房源信息(四)