爬取小豬網站住房資訊並把結果儲存到資料庫中
阿新 • • 發佈:2018-12-01
from bs4 import BeautifulSoup
import requests, pymongo
#啟用MongoDB
client = pymongo.MongoClient('localhost', 27017)
#給資料庫命名
xiaozhu = client['xiaozhu']
#建立一個表單
bnb_info = xiaozhu['bnb_info']
#定義一個能獲取多頁資訊的函式
def getMorePage(pages):
for page in range(1, pages+1):
url = 'http://bj.xiaozhu.com/search-duanzufang-p{}-0/'
wb_data = requests.get(url.format(page))
soup = BeautifulSoup(wb_data.text, 'html.parser')
#房子標題、價格
titles = soup.select("span.result_title")
prices = soup.select("span.result_price > i")
# 打包存入字典
for title, price in zip(titles, prices):
data = {
'title' :title.get_text(),
'price':price.get_text()
}
#將資料寫入資料庫==填寫Excel表格每一行
bnb_info,insert_one(data)
print("完成")
getMorePage(3)
# 從資料庫中篩選資訊
for i in bnb_info.find():
if i['price'] >= 500:
#篩選出價格大於等於500的資訊
print(i)
#上訴程式碼也可寫成
for i in bnb_info({'price':{'$gte':500}}):
print(i)
補充:
$lt less than <
$lte less than equal <=
$gt greater than >
$gte …… >=
$ne not than !=