大家好,又見面了,我是你們的朋友全棧君。
從鏈家網站爬蟲石家莊符合條件的房源信息,並保存到文件,房源信息包括名稱、建築面積、總價、所在區域、套內面積等。其中所在區域、套內面積需要在詳情頁獲取計算。
主要使用了requests+BeautifulSoup第三方模塊,具體使用方法可百度。
第一版是2019年4月份寫的,當前已失效。
第二版是12月份寫的。
第一版:
#!/usr/bin/python
from bs4 import BeautifulSoup
import requests
def getHouseList(url):
house =[]
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER'}
#get從網頁獲取信息
res = requests.get(url,headers=headers)
#解析內容
soup = BeautifulSoup(res.content,'lxml')
#房源title
housename_divs = soup.find_all('div',class_='title')
for housename_div in housename_divs:
housename_as=housename_div.find_all('a')
for housename_a in housename_as:
housename=[]
#標題
housename.append(housename_a.get_text())
#超鏈接
housename.append(housename_a['href'])
house.append(housename)
huseinfo_divs = soup.find_all('div',class_='houseInfo')
for i in range(len(huseinfo_divs)):
info = huseinfo_divs[i].get_text()
infos = info.split('|')
#小區名稱
house[i].append(infos[0])
#戶型
house[i].append(infos[1])
#平米
house[i].append(infos[2])
#查詢總價
house_prices = soup.find_all('div',class_='totalPrice')
for i in range(len(house_prices)):
#價格
price = house_prices[i].get_text()
house[i].append(price)
return house
#爬取房屋詳細信息:所在區域、套內面積
def houseinfo(url):
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER'}
res = requests.get(url,headers=headers)
soup = BeautifulSoup(res.content,'lxml')
msg =[]
#所在區域
areainfos = soup.find_all('span',class_='info')
for areainfo in areainfos:
#只需要獲取第一個a標簽的內容即可
area = areainfo.find('a')
if(not area):
continue
hrefStr = area['href']
if(hrefStr.startswith('javascript')):
continue
msg.append(area.get_text())
break
#根據房屋戶型計算套內面積
infolist = soup.find_all('div',id='infoList')
num = []
for info in infolist:
cols = info.find_all('div',class_='col')
for i in cols:
pingmi = i.get_text()
try:
a = float(pingmi[:-2])
num.append(a)
except ValueError:
continue
msg.append(sum(num))
return msg
#將房源信息寫入txt文件
def writeFile(houseinfo):
f = open('d:/房源.txt','a',encoding='utf8')
# houseinfo.join('\n')
f.write(houseinfo+'\n')
f.close()
#主函數
def main():
for i in range(1,100):
print('-----分隔符',i,'-------')
if i==1:
url ='https://sjz.lianjia.com/ershoufang/hy1f2f5sf1l3l2l4a2a3a4/'
else:
url='https://sjz.lianjia.com/ershoufang/pg'+str(i)+'hy1f2f5sf1l3l2l4a2a3a4/'
houses =getHouseList(url)
for house in houses:
link = house[1]
if(not link.startswith('http')):
continue
mianji = houseinfo(link)
#將套內面積、所在區域增加到房源信息
house.extend(mianji)
print(house)
info = " ".join([str(x) for x in house])
writeFile(info)
if __name__ == '__main__':
main()
從鏈家網站查詢到8849條房源信息,但是頁面只能顯示31(每頁數量)*100(總頁碼)=3100條房源,其他沒找到。
第二版:
獲取某個小區的房源信息,並寫入excel。
#!/usr/bin/python
from bs4 import BeautifulSoup
import requests
import xlwt
def getHouseList(url):
house =[]
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER'}
#get從網頁獲取信息
res = requests.get(url,headers=headers)
#解析內容
soup = BeautifulSoup(res.content,'html.parser')
#房源title
housename_divs = soup.find_all('div',class_='title')
for housename_div in housename_divs:
housename_as=housename_div.find_all('a')
for housename_a in housename_as:
housename=[]
#標題
housename.append(housename_a.get_text())
#超鏈接
housename.append(housename_a.get('href'))
house.append(housename)
huseinfo_divs = soup.find_all('div',class_='houseInfo')
for i in range(len(huseinfo_divs)):
info = huseinfo_divs[i].get_text()
infos = info.split('|')
#小區名稱
house[i].append(infos[0])
#戶型
house[i].append(infos[1])
#平米
house[i].append(infos[2])
#查詢總價
house_prices = soup.find_all('div',class_='totalPrice')
for i in range(len(house_prices)):
#價格
price = house_prices[i].get_text()
house[i].append(price)
return house
#爬取房屋詳細信息:所在區域、套內面積
def houseinfo(url):
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER'}
res = requests.get(url,headers=headers)
soup = BeautifulSoup(res.content,'html.parser')
msg =[]
#所在區域
areainfos = soup.find_all('span',class_='info')
for areainfo in areainfos:
#只需要獲取第一個a標簽的內容即可
area = areainfo.find('a')
if(not area):
continue
hrefStr = area['href']
if(hrefStr.startswith('javascript')):
continue
msg.append(area.get_text())
break
#根據房屋戶型計算套內面積
infolist = soup.find_all('div',id='infoList')
num = []
for info in infolist:
cols = info.find_all('div',class_='col')
for i in cols:
pingmi = i.get_text()
try:
a = float(pingmi[:-2])
num.append(a)
except ValueError:
continue
msg.append(sum(num))
return msg
#將房源信息寫入excel文件
def writeExcel(excelPath,houses):
workbook = xlwt.Workbook()
#獲取第一個sheet頁
sheet = workbook.add_sheet('git')
row0=['標題','鏈接地址','戶型','面積','朝向','總價','所屬區域','套內面積']
for i in range(0,len(row0)):
sheet.write(0,i,row0[i])
for i in range(0,len(houses)):
house = houses[i]
print(house)
for j in range(0,len(house)):
sheet.write(i+1,j,house[j])
workbook.save(excelPath)
#主函數
def main():
data = []
for i in range(1,5):
print('-----分隔符',i,'-------')
if i==1:
url ='https://sjz.lianjia.com/ershoufang/l2rs%E5%92%8C%E5%B9%B3%E4%B8%96%E5%AE%B6/'
else:
url='https://sjz.lianjia.com/ershoufang/pg'+str(i)+'l2rs%E5%92%8C%E5%B9%B3%E4%B8%96%E5%AE%B6/'
houses =getHouseList(url)
for house in houses:
link = house[1]
if(not link or not link.startswith('http')):
continue
mianji = houseinfo(link)
#將套內面積、所在區域增加到房源信息
house.extend(mianji)
data.extend(houses)
writeExcel('d:/house.xls',data)
if __name__ == '__main__':
main()
發布者:全棧程序員棧長,轉載請注明出處:https://javaforall.cn/133817.html原文鏈接:https://javaforall.cn