記一次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('存儲(chǔ)到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時(shí)會(huì)報(bào)錯(cuò)
pass
else:
results = parser_room_page(room_html)
for result in results:
save_to_mongo(result)
if __name__ == '__main__':
pool = Pool() # 使用多進(jìn)程提高爬取效率
pool.map(main, [i for i in range(1, 258)])
在寫爬取代碼過程中遇到了兩個(gè)問題:
(一)在get_room_url(html)函數(shù)中,開始是想直接return每個(gè)租房信息的room_url,但是return不同于print,函數(shù)運(yùn)行到return時(shí)就會(huì)結(jié)束該函數(shù),這樣就只能返回每頁第一個(gè)租房room_url。解決辦法是:return 包含每頁所有room_url的generator生成器,在main函數(shù)中用for循環(huán)遍歷,再從每個(gè)room_url中獲取href,傳入到get_one_page_html(room_url_href)中進(jìn)行解析。
(二)沒有寫第76行的if語句,我默認(rèn)get_one_page_html(room_url_href)返回的room_html不為空,因此出現(xiàn)multiprocessing.pool.RemoteTraceback報(bào)錯(cuò):

上圖中顯示markup為None情況下報(bào)錯(cuò),點(diǎn)擊藍(lán)色"F:\ProgramFiles\anaconda3\lib\site-packages\bs4\__init__.py"發(fā)現(xiàn)markup為room_html,即部分room_html出現(xiàn)None情況。要解決這個(gè)問題,必須讓代碼跳過room_html is None的情況,因此添加 if 語句解決了這個(gè)問題。
最終成功爬取某租房公寓深圳市258頁共4755條租房信息,為下一步進(jìn)行數(shù)據(jù)分析做準(zhǔn)備。

其中單條信息:

以上就是記一次python 爬蟲爬取深圳租房信息的過程及遇到的問題的詳細(xì)內(nèi)容,更多關(guān)于python 爬蟲的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python+Requests+PyTest+Excel+Allure?接口自動(dòng)化測試實(shí)戰(zhàn)
本文主要介紹了Python+Requests+PyTest+Excel+Allure?接口自動(dòng)化測試實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Python網(wǎng)絡(luò)爬蟲神器PyQuery的基本使用教程
這篇文章主要給大家介紹了關(guān)于Python網(wǎng)絡(luò)爬蟲神器PyQuery的基本使用教程,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)使用PyQuery具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02
Python 2種方法求某個(gè)范圍內(nèi)的所有素?cái)?shù)(質(zhì)數(shù))
這篇文章主要介紹了Python 2種方法求某個(gè)范圍內(nèi)的所有素?cái)?shù)(質(zhì)數(shù)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
加速Python代碼執(zhí)行利器使用實(shí)例探究
這篇文章主要為大家介紹了加速Python代碼執(zhí)行的利器使用實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
PageFactory設(shè)計(jì)模式基于python實(shí)現(xiàn)
這篇文章主要介紹了PageFactory設(shè)計(jì)模式基于python實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Python用20行代碼實(shí)現(xiàn)批量摳圖功能
在日常的工作和生活中,我們經(jīng)常會(huì)遇到需要摳圖的場景,即便是只有一張圖片需要摳,也會(huì)摳得我們不耐煩。本文將為大家分享一個(gè)20行代碼就能實(shí)現(xiàn)是批量摳圖,需要的可以參考一下2022-05-05

