記一次python 爬蟲爬取深圳租房信息的過程及遇到的問題
為了分析深圳市所有長租、短租公寓的信息,爬取了某租房公寓網(wǎng)站上深圳區(qū)域所有在租公寓信息,以下記錄了爬取過程以及爬取過程中遇到的問題:
爬取代碼:
import requests from requests.exceptions import RequestException from pyquery import PyQuery as pq from bs4 import BeautifulSoup import pymongo from config import * from multiprocessing import Pool client = pymongo.MongoClient(MONGO_URL) # 申明連接對象 db = client[MONGO_DB] # 申明數(shù)據(jù)庫 def get_one_page_html(url): # 獲取網(wǎng)站每一頁的html headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " "Chrome/85.0.4183.121 Safari/537.36" } try: response = requests.get(url, headers=headers) if response.status_code == 200: return response.text else: return None except RequestException: return None def get_room_url(html): # 獲取當(dāng)前頁面上所有room_info的url doc = pq(html) room_urls = doc('.r_lbx .r_lbx_cen .r_lbx_cena a').items() return room_urls def parser_room_page(room_html): soup = BeautifulSoup(room_html, 'lxml') title = soup.h1.text price = soup.find('div', {'class': 'room-price-sale'}).text[:-3] x = soup.find_all('div', {'class': 'room-list'}) area = x[0].text[7:-11] # 面積 bianhao = x[1].text[4:] house_type = x[2].text.strip()[3:7] # 戶型 floor = x[5].text[4:-2] # 樓層 location1 = x[6].find_all('a')[0].text # 分區(qū) location2 = x[6].find_all('a')[1].text location3 = x[6].find_all('a')[2].text subway = x[7].text[4:] addition = soup.find_all('div', {'class': 'room-title'})[0].text yield { 'title': title, 'price': price, 'area': area, 'bianhao': bianhao, 'house_type': house_type, 'floor': floor, 'location1': location1, 'location2': location2, 'location3': location3, 'subway': subway, 'addition': addition } def save_to_mongo(result): if db[MONGO_TABLE].insert_one(result): print('存儲到mongodb成功', result) return True return False def main(page): url = 'http://www.xxxxx.com/room/sz?page=' + str(page) # url就不粘啦,嘻嘻 html = get_one_page_html(url) room_urls = get_room_url(html) for room_url in room_urls: room_url_href = room_url.attr('href') room_html = get_one_page_html(room_url_href) if room_html is None: # 非常重要,否則room_html為None時會報錯 pass else: results = parser_room_page(room_html) for result in results: save_to_mongo(result) if __name__ == '__main__': pool = Pool() # 使用多進程提高爬取效率 pool.map(main, [i for i in range(1, 258)])
在寫爬取代碼過程中遇到了兩個問題:
(一)在get_room_url(html)函數(shù)中,開始是想直接return每個租房信息的room_url,但是return不同于print,函數(shù)運行到return時就會結(jié)束該函數(shù),這樣就只能返回每頁第一個租房room_url。解決辦法是:return 包含每頁所有room_url的generator生成器,在main函數(shù)中用for循環(huán)遍歷,再從每個room_url中獲取href,傳入到get_one_page_html(room_url_href)中進行解析。
(二)沒有寫第76行的if語句,我默認(rèn)get_one_page_html(room_url_href)返回的room_html不為空,因此出現(xiàn)multiprocessing.pool.RemoteTraceback報錯:
上圖中顯示markup為None情況下報錯,點擊藍色"F:\ProgramFiles\anaconda3\lib\site-packages\bs4\__init__.py"發(fā)現(xiàn)markup為room_html,即部分room_html出現(xiàn)None情況。要解決這個問題,必須讓代碼跳過room_html is None的情況,因此添加 if 語句解決了這個問題。
最終成功爬取某租房公寓深圳市258頁共4755條租房信息,為下一步進行數(shù)據(jù)分析做準(zhǔn)備。
其中單條信息:
以上就是記一次python 爬蟲爬取深圳租房信息的過程及遇到的問題的詳細(xì)內(nèi)容,更多關(guān)于python 爬蟲的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python 字符串和整數(shù)的轉(zhuǎn)換方法
今天小編就為大家分享一篇python 字符串和整數(shù)的轉(zhuǎn)換方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06Django ORM多對多查詢方法(自定義第三張表&ManyToManyField)
今天小編就為大家分享一篇Django ORM多對多查詢方法(自定義第三張表&ManyToManyField),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python實現(xiàn)獲取前100組勾股數(shù)的方法示例
這篇文章主要介紹了Python實現(xiàn)獲取前100組勾股數(shù)的方法,涉及Python數(shù)值計算與判斷相關(guān)操作技巧,需要的朋友可以參考下2018-05-05Python中的getattr、__getattr__、__getattribute__、__get__詳解
這篇文章主要為大家介紹了Python中的getattr,__getattr__,__getattribute__和__get__,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-12-12python實現(xiàn)在內(nèi)存中讀寫str和二進制數(shù)據(jù)代碼
這篇文章主要介紹了python實現(xiàn)在內(nèi)存中讀寫str和二進制數(shù)據(jù)代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04