現(xiàn)在開始正式進(jìn)行爬蟲書寫首先,需要分析一下要爬取的網(wǎng)站的結(jié)構(gòu):作為一名河南的學(xué)生,那就看看鄭州的二手房信息吧!
在上面這個(gè)頁面中,我們可以看到一條條的房源信息,由上可以看到網(wǎng)頁一條條的房源信息,點(diǎn)擊進(jìn)去后就會(huì)發(fā)現(xiàn):
房源的詳細(xì)信息。OK!那么我們要干嘛呢,就是把鄭州這個(gè)地區(qū)的二手房房源信息都能拿到手,可以保存到數(shù)據(jù)庫中,用來干嘛呢,作為一個(gè)地理人,還是有點(diǎn)用處的,這次就不說了好,正式開始,首先我采用python3.6 中的requests,BeautifulSoup模塊來進(jìn)行爬取頁面,首先由requests模塊進(jìn)行請(qǐng)求:
# 網(wǎng)頁的請(qǐng)求頭 header = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36' } # url鏈接 url = 'https://zhengzhou.anjuke.com/sale/' response = requests.get(url, headers=header) print(response.text)
執(zhí)行后就會(huì)得到這個(gè)網(wǎng)站的html代碼了
通過分析可以得到每個(gè)房源都在class="list-item"的 li 標(biāo)簽中,那么我們就可以根據(jù)BeautifulSoup包進(jìn)行提取
# 通過BeautifulSoup進(jìn)行解析出每個(gè)房源詳細(xì)列表并進(jìn)行打印 soup = BeautifulSoup(response.text, 'html.parser') result_li = soup.find_all('li', {'class': 'list-item'}) for i in result_li: print(i)
通過打印就能進(jìn)一步減少了code量,好,繼續(xù)提取
# 通過BeautifulSoup進(jìn)行解析出每個(gè)房源詳細(xì)列表并進(jìn)行打印 soup = BeautifulSoup(response.text, 'html.parser') result_li = soup.find_all('li', {'class': 'list-item'}) # 進(jìn)行循環(huán)遍歷其中的房源詳細(xì)列表 for i in result_li: # 由于BeautifulSoup傳入的必須為字符串,所以進(jìn)行轉(zhuǎn)換 page_url = str(i) soup = BeautifulSoup(page_url, 'html.parser') # 由于通過class解析的為一個(gè)列表,所以只需要第一個(gè)參數(shù) result_href = soup.find_all('a', {'class': 'houseListTitle'})[0] print(result_href.attrs['href'])
這樣,我們就能看到一個(gè)個(gè)的url了,是不是很喜歡
好了,按正常的邏輯就要進(jìn)入頁面開始分析詳細(xì)頁面了,但是爬取完后如何進(jìn)行下一頁的爬取呢所以,我們就需要先分析該頁面是否有下一頁
同樣的方法就可以發(fā)現(xiàn)下一頁同樣是如此的簡單,那么咱們就可以還是按原來的配方原來的味道繼續(xù)
# 進(jìn)行下一頁的爬取 result_next_page = soup.find_all('a', {'class': 'aNxt'}) if len(result_next_page) != 0: print(result_next_page[0].attrs['href']) else: print('沒有下一頁了')
因?yàn)楫?dāng)存在下一頁的時(shí)候,網(wǎng)頁中就是一個(gè)a標(biāo)簽,如果沒有的話,就會(huì)成為i標(biāo)簽了,所以這樣的就行,因此,我們就能完善一下,將以上這些封裝為一個(gè)函數(shù)
import requests from bs4 import BeautifulSoup # 網(wǎng)頁的請(qǐng)求頭 header = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36' } def get_page(url): response = requests.get(url, headers=header) # 通過BeautifulSoup進(jìn)行解析出每個(gè)房源詳細(xì)列表并進(jìn)行打印 soup = BeautifulSoup(response.text, 'html.parser') result_li = soup.find_all('li', {'class': 'list-item'}) # 進(jìn)行下一頁的爬取 result_next_page = soup.find_all('a', {'class': 'aNxt'}) if len(result_next_page) != 0: # 函數(shù)進(jìn)行遞歸 get_page(result_next_page[0].attrs['href']) else: print('沒有下一頁了') # 進(jìn)行循環(huán)遍歷其中的房源詳細(xì)列表 for i in result_li: # 由于BeautifulSoup傳入的必須為字符串,所以進(jìn)行轉(zhuǎn)換 page_url = str(i) soup = BeautifulSoup(page_url, 'html.parser') # 由于通過class解析的為一個(gè)列表,所以只需要第一個(gè)參數(shù) result_href = soup.find_all('a', {'class': 'houseListTitle'})[0] # 先不做分析,等一會(huì)進(jìn)行詳細(xì)頁面函數(shù)完成后進(jìn)行調(diào)用 print(result_href.attrs['href']) if __name__ == '__main__': # url鏈接 url = 'https://zhengzhou.anjuke.com/sale/' # 頁面爬取函數(shù)調(diào)用 get_page(url)
好了,那么咱們就開始詳細(xì)頁面的爬取了
哎,怎么動(dòng)不動(dòng)就要斷電了,大學(xué)的坑啊,先把結(jié)果附上,閑了在補(bǔ)充,
import requests from bs4 import BeautifulSoup # 網(wǎng)頁的請(qǐng)求頭 header = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36' } def get_page(url): response = requests.get(url, headers=header) # 通過BeautifulSoup進(jìn)行解析出每個(gè)房源詳細(xì)列表并進(jìn)行打印 soup_idex = BeautifulSoup(response.text, 'html.parser') result_li = soup_idex.find_all('li', {'class': 'list-item'}) # 進(jìn)行循環(huán)遍歷其中的房源詳細(xì)列表 for i in result_li: # 由于BeautifulSoup傳入的必須為字符串,所以進(jìn)行轉(zhuǎn)換 page_url = str(i) soup = BeautifulSoup(page_url, 'html.parser') # 由于通過class解析的為一個(gè)列表,所以只需要第一個(gè)參數(shù) result_href = soup.find_all('a', {'class': 'houseListTitle'})[0] # 詳細(xì)頁面的函數(shù)調(diào)用 get_page_detail(result_href.attrs['href']) # 進(jìn)行下一頁的爬取 result_next_page = soup_idex.find_all('a', {'class': 'aNxt'}) if len(result_next_page) != 0: # 函數(shù)進(jìn)行遞歸 get_page(result_next_page[0].attrs['href']) else: print('沒有下一頁了') # 進(jìn)行字符串中空格,換行,tab鍵的替換及刪除字符串兩邊的空格刪除 def my_strip(s): return str(s).replace(" ", "").replace(" ", "").replace(" ", "").strip() # 由于頻繁進(jìn)行BeautifulSoup的使用,封裝一下,很雞肋 def my_Beautifulsoup(response): return BeautifulSoup(str(response), 'html.parser') # 詳細(xì)頁面的爬取 def get_page_detail(url): response = requests.get(url, headers=header) if response.status_code == 200: soup = BeautifulSoup(response.text, 'html.parser') # 標(biāo)題什么的一大堆,哈哈 result_title = soup.find_all('h3', {'class': 'long-title'})[0] result_price = soup.find_all('span', {'class': 'light info-tag'})[0] result_house_1 = soup.find_all('p', {'class': 'first-col detail-col'}) result_house_2 = soup.find_all('p', {'class': 'second-col detail-col'}) result_house_3 = soup.find_all('p', {'class': 'third-col detail-col'}) soup_1 = my_Beautifulsoup(result_house_1) soup_2 = my_Beautifulsoup(result_house_2) soup_3 = my_Beautifulsoup(result_house_3) result_house_tar_1 = soup_1.find_all('dd') result_house_tar_2 = soup_2.find_all('dd') result_house_tar_3 = soup_3.find_all('dd') ''' 文博公寓,省實(shí)驗(yàn)中學(xué),首付只需70萬,大三房,誠心賣,價(jià)可談 270萬 宇泰文博公寓 金水-花園路-文博東路4號(hào) 2010年 普通住宅 3室2廳2衛(wèi) 140平方米 南北 中層(共32層) 精裝修 19285元/m? 81.00萬 ''' print(my_strip(result_title.text), my_strip(result_price.text)) print(my_strip(result_house_tar_1[0].text), my_strip(my_Beautifulsoup(result_house_tar_1[1]).find_all('p')[0].text), my_strip(result_house_tar_1[2].text), my_strip(result_house_tar_1[3].text)) print(my_strip(result_house_tar_2[0].text), my_strip(result_house_tar_2[1].text), my_strip(result_house_tar_2[2].text), my_strip(result_house_tar_2[3].text)) print(my_strip(result_house_tar_3[0].text), my_strip(result_house_tar_3[1].text), my_strip(result_house_tar_3[2].text)) if __name__ == '__main__': # url鏈接 url = 'https://zhengzhou.anjuke.com/sale/' # 頁面爬取函數(shù)調(diào)用 get_page(url)
由于自己邊寫博客,邊寫的代碼,所以get_page函數(shù)中進(jìn)行了一些改變,就是下一頁的遞歸調(diào)用需要放在函數(shù)后面,以及進(jìn)行封裝了兩個(gè)函數(shù)沒有介紹,
而且數(shù)據(jù)存儲(chǔ)到mysql也沒有寫,所以后期會(huì)繼續(xù)跟進(jìn)的,thank you!!!
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com