現在開始正式進行爬蟲書寫首先,需要分析一下要爬取的網站的結構:作為一名河南的學生,那就看看鄭州的二手房信息吧!
在上面這個頁面中,我們可以看到一條條的房源信息,由上可以看到網頁一條條的房源信息,點擊進去后就會發現:
房源的詳細信息。OK!那么我們要干嘛呢,就是把鄭州這個地區的二手房房源信息都能拿到手,可以保存到數據庫中,用來干嘛呢,作為一個地理人,還是有點用處的,這次就不說了好,正式開始,首先我采用python3.6 中的requests,BeautifulSoup模塊來進行爬取頁面,首先由requests模塊進行請求:
# 網頁的請求頭 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)
執行后就會得到這個網站的html代碼了
通過分析可以得到每個房源都在class="list-item"的 li 標簽中,那么我們就可以根據BeautifulSoup包進行提取
# 通過BeautifulSoup進行解析出每個房源詳細列表并進行打印 soup = BeautifulSoup(response.text, 'html.parser') result_li = soup.find_all('li', {'class': 'list-item'}) for i in result_li: print(i)
通過打印就能進一步減少了code量,好,繼續提取
# 通過BeautifulSoup進行解析出每個房源詳細列表并進行打印 soup = BeautifulSoup(response.text, 'html.parser') result_li = soup.find_all('li', {'class': 'list-item'}) # 進行循環遍歷其中的房源詳細列表 for i in result_li: # 由于BeautifulSoup傳入的必須為字符串,所以進行轉換 page_url = str(i) soup = BeautifulSoup(page_url, 'html.parser') # 由于通過class解析的為一個列表,所以只需要第一個參數 result_href = soup.find_all('a', {'class': 'houseListTitle'})[0] print(result_href.attrs['href'])
這樣,我們就能看到一個個的url了,是不是很喜歡
好了,按正常的邏輯就要進入頁面開始分析詳細頁面了,但是爬取完后如何進行下一頁的爬取呢所以,我們就需要先分析該頁面是否有下一頁
同樣的方法就可以發現下一頁同樣是如此的簡單,那么咱們就可以還是按原來的配方原來的味道繼續
# 進行下一頁的爬取 result_next_page = soup.find_all('a', {'class': 'aNxt'}) if len(result_next_page) != 0: print(result_next_page[0].attrs['href']) else: print('沒有下一頁了')
因為當存在下一頁的時候,網頁中就是一個a標簽,如果沒有的話,就會成為i標簽了,所以這樣的就行,因此,我們就能完善一下,將以上這些封裝為一個函數
import requests from bs4 import BeautifulSoup # 網頁的請求頭 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進行解析出每個房源詳細列表并進行打印 soup = BeautifulSoup(response.text, 'html.parser') result_li = soup.find_all('li', {'class': 'list-item'}) # 進行下一頁的爬取 result_next_page = soup.find_all('a', {'class': 'aNxt'}) if len(result_next_page) != 0: # 函數進行遞歸 get_page(result_next_page[0].attrs['href']) else: print('沒有下一頁了') # 進行循環遍歷其中的房源詳細列表 for i in result_li: # 由于BeautifulSoup傳入的必須為字符串,所以進行轉換 page_url = str(i) soup = BeautifulSoup(page_url, 'html.parser') # 由于通過class解析的為一個列表,所以只需要第一個參數 result_href = soup.find_all('a', {'class': 'houseListTitle'})[0] # 先不做分析,等一會進行詳細頁面函數完成后進行調用 print(result_href.attrs['href']) if __name__ == '__main__': # url鏈接 url = 'https://zhengzhou.anjuke.com/sale/' # 頁面爬取函數調用 get_page(url)
好了,那么咱們就開始詳細頁面的爬取了
哎,怎么動不動就要斷電了,大學的坑啊,先把結果附上,閑了在補充,
import requests from bs4 import BeautifulSoup # 網頁的請求頭 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進行解析出每個房源詳細列表并進行打印 soup_idex = BeautifulSoup(response.text, 'html.parser') result_li = soup_idex.find_all('li', {'class': 'list-item'}) # 進行循環遍歷其中的房源詳細列表 for i in result_li: # 由于BeautifulSoup傳入的必須為字符串,所以進行轉換 page_url = str(i) soup = BeautifulSoup(page_url, 'html.parser') # 由于通過class解析的為一個列表,所以只需要第一個參數 result_href = soup.find_all('a', {'class': 'houseListTitle'})[0] # 詳細頁面的函數調用 get_page_detail(result_href.attrs['href']) # 進行下一頁的爬取 result_next_page = soup_idex.find_all('a', {'class': 'aNxt'}) if len(result_next_page) != 0: # 函數進行遞歸 get_page(result_next_page[0].attrs['href']) else: print('沒有下一頁了') # 進行字符串中空格,換行,tab鍵的替換及刪除字符串兩邊的空格刪除 def my_strip(s): return str(s).replace(" ", "").replace(" ", "").replace(" ", "").strip() # 由于頻繁進行BeautifulSoup的使用,封裝一下,很雞肋 def my_Beautifulsoup(response): return BeautifulSoup(str(response), 'html.parser') # 詳細頁面的爬取 def get_page_detail(url): response = requests.get(url, headers=header) if response.status_code == 200: soup = BeautifulSoup(response.text, 'html.parser') # 標題什么的一大堆,哈哈 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') ''' 文博公寓,省實驗中學,首付只需70萬,大三房,誠心賣,價可談 270萬 宇泰文博公寓 金水-花園路-文博東路4號 2010年 普通住宅 3室2廳2衛 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/' # 頁面爬取函數調用 get_page(url)
由于自己邊寫博客,邊寫的代碼,所以get_page函數中進行了一些改變,就是下一頁的遞歸調用需要放在函數后面,以及進行封裝了兩個函數沒有介紹,
而且數據存儲到mysql也沒有寫,所以后期會繼續跟進的,thank you!!!
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com