python爬蟲--房產數據爬取並保存本地


 
         

import requests
import csv
from bs4 import BeautifulSoup
headers={'user-agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36 Maxthon/5.2.6.1000'}
for i in range(1,10):
link='https://fz.anjuke.com/sale/p'+str(i)+'/#filtersort'
r=requests.get(link,headers=headers)
print(str(i + 1), "頁響應狀態碼:", r.status_code)
soup=BeautifulSoup(r.text,'lxml')
house_list=soup.find_all('li',class_="list-item")
with open('test.csv', 'a',newline='',encoding='utf-8-sig')as csvfile:
w=csv.writer(csvfile)
w.writerow(('標題','價格','均價','面積','樓層'))
for house in house_list:
temp = []
name=house.find('div',class_='house-title').a.text.strip()
price=house.find('div',class_='pro-price').contents[1].text.strip()
price_ave=house.find('div',class_='pro-price').contents[2].text.strip()
area=house.find('div',class_='details-item').span.text
floor=house.find('div',class_='details-item').contents[5].text
temp=[name,price,price_ave,area,floor]
print(temp)
w.writerow(temp)

 

幾個注意點:

1、with open('test.csv', 'a',newline='',encoding='utf-8-sig')as csvfile:,注意utf8轉碼,否則數據保存本地會為亂碼形式

2、插入標題的方式,數組的寫入

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM