首頁  >  文章  >  後端開發  >  python爬取安居客二手房網站資料方法分享

python爬取安居客二手房網站資料方法分享

小云云
小云云原創
2018-01-09 13:20:283537瀏覽

本文主要為大家帶來一篇python爬取安居客二手房網站資料(實例講解)。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧,希望能幫助大家。

現在開始正式進行爬蟲書寫首先,需要分析一下要爬取的網站的結構:作為一名河南的學生,那就看看鄭州的二手房信息吧!

在上面這個頁面中,我們可以看到一則的房源訊息,由上可以看到網頁一則的房源訊息,點選進去後就會發現:

房源的詳細資料。 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("\n", "").replace("\t", "").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!!!

相關推薦:

python爬取文章實例教學

關於python爬取的文章推薦10篇

#分享一種Python爬取網易雲音樂熱門評論的方法

以上是python爬取安居客二手房網站資料方法分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn