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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

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)容,希望文章能夠幫你解決所遇到的問題。

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

歡迎分享!

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

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