如何高效地爬取链家的房源信息(二)
“Python實現(xiàn)的鏈家網(wǎng)站的爬蟲第二部分。”
本系列文將以鏈家南京站為例,使用Python實現(xiàn)鏈家二手房源信息的爬蟲,將數(shù)據(jù)爬取,并存入數(shù)據(jù)庫中,以便使用。
本系列第一部分:
如何高效地爬取鏈家的房源信息(一)
本文是第二部分,爬取小區(qū)信息并存入數(shù)據(jù)庫,部分代碼依賴于第一部分。
在前文中已經(jīng)獲取了大區(qū)域的URL,接下來只需要遍歷各個URL即可爬下所有小區(qū)信息:
# 爬下所有的小區(qū)信息
for regionurl in regionurls:
? ? do_xiaoqu_spider(db_xq, regionurl)
對一個區(qū)內(nèi)的所有小區(qū)進行爬取,需要分頁:
def do_xiaoqu_spider(db_xq, url=u"https://nj.lianjia.com/xiaoqu/gulou/"):
? ? """
? ? 爬取大區(qū)域中的所有小區(qū)信息
? ? """
? ? try:
? ? ? ? req = urllib.request.Request(url, headers=hds[random.randint(0, len(hds) - 1)])
? ? ? ? source_code = urllib.request.urlopen(req, timeout=5).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)
? ? ? ? return
? ? except Exception as e:
? ? ? ? print(e)
? ? ? ? return
? ? d = "d="+soup.find('div', {'class': 'page-box house-lst-page-box'}).get('page-data')
? ? loc = {}
? ? glb = {}
? ? exec(d, glb, loc);
? ? total_pages = loc['d']['totalPage']
? ? threads = []
? ? for i in range(total_pages):
? ? ? ? url_page = url+u"pg%d/" % (i + 1);
? ? ? ? print(url_page);
? ? ? ? t = threading.Thread(target=xiaoqu_spider, args=(db_xq, url_page))
? ? ? ? threads.append(t)
? ? for t in threads:
? ? ? ? t.start()
? ? for t in threads:
? ? ? ? t.join()
? ? print(u"爬下了 %s 區(qū)全部的小區(qū)信息" % url)
爬取單個頁面內(nèi)的小區(qū)信息:
def xiaoqu_spider(db_xq, url_page=u"https://nj.lianjia.com/xiaoqu/gulou/pg1/"):
? ? """
? ? 爬取頁面鏈接中的小區(qū)信息
? ? """
? ? 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)
? ? ? ? exit(-1)
? ? except Exception as e:
? ? ? ? print(e)
? ? ? ? exit(-1)
? ? xiaoqu_list = soup.findAll('li', {'class': 'clear xiaoquListItem'})
? ? for xq in xiaoqu_list:
? ? ? ? info_dict = {}
? ? ? ? title = xq.find('div', {'class': 'title'});
? ? ? ? info_dict.update({u'小區(qū)名稱': title.text})
? ? ? ? d=title.findAll('a')
? ? ? ? for item in d:
? ? ? ? ? ? href = item['href'];
? ? ? ? ? ? info_dict.update({u'url': href})
? ? ? ? postioninfo = xq.find('div', {'class': 'positionInfo'}).renderContents().strip().decode('utf-8');
? ? ? ? content = "".join(postioninfo.split())
? ? ? ? info = re.match(r".+district.+>(.+)</a>.+bizcircle.+>(.+)</a>(.+)", content)
? ? ? ? if info:
? ? ? ? ? ? info = info.groups()
? ? ? ? ? ? info_dict.update({u'大區(qū)域': info[0]})
? ? ? ? ? ? info_dict.update({u'小區(qū)域': info[1]})
? ? ? ? ? ? info_dict.update({u'建造時間': info[2]})
? ? ? ? command = gen_xiaoqu_insert_command(info_dict)
? ? ? ? db_xq.execute(command, 1)
爬取的小區(qū)信息將被存儲到數(shù)據(jù)庫表中,供后續(xù)使用。
在接下來將說明如何爬取在售二手房信息、歷史成交二手房信息,敬請期待。
長按進行關(guān)注。
總結(jié)
以上是生活随笔為你收集整理的如何高效地爬取链家的房源信息(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何高效地爬取链家的房源信息(一)
- 下一篇: 如何高效地爬取链家的房源信息(三)