實時爬取二手房資料
#coding=utf-8
import time
import json
from multiprocessing import Pool
import requests
from bs4 import BeautifulSoup
import re
import pandas as pd
import pymongo
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def generate_allurl(user_in_nub, user_in_city): # 生成url
url = 'http://' + user_in_city + '.lianjia.com/ershoufang/pg{}/'
for url_next in range(1, int(user_in_nub)):
writer_to_text(url.format(url_next))
yield url.format(url_next)
def get_allurl(generate_allurl): # 分析url解析出每一頁的詳細url
get_url = requests.get(generate_allurl, 'lxml')
if get_url.status_code == 200:
#re_set = re.compile('<li.*?class="clear">.*?<a.*?class="img.*?".*?href="(.*?)"')
re_set = re.compile('target="_blank" href="(/ershoufang/.*?)" class="img js_triggerGray"')
re_get = re.findall(re_set, get_url.text)
return re_get
def open_url(re_get): # 分析詳細url獲取所需資訊
res = requests.get(re_get)
if res.status_code == 200:
info = {}
soup = BeautifulSoup(res.text, 'lxml' )
info['標題'] = soup.select('.header-title')[0].text
info['總價'] = soup.select('.price-num')[0].text + '萬'
info['每平方售價'] = soup.select('.u-bold')[0].text + '元/平'
info['房間']=soup.select('.main-item')[0].text.split()[0]
info['裝修'] = soup.select('.main-item')[0].text.split()[1]
info['朝向'] = soup.select('.main-item')[1].text.split()[0]
info['樓層'] = soup.select('.main-item')[1].text.split()[1]
info['面積'] = soup.select('.main-item')[2].text.split()[0]
info['建造時間'] = soup.select('.main-item')[2].text.split()[1]
info['小區名稱'] = soup.select('.u-link')[0].text
info['所在區域'] = soup.select('.u-link')[1].text + ':' + soup.select('.u-link')[2].text
info['鏈家編號'] = str(re_get)[33:].rsplit('.html')[0]
#for i in soup.select('.base li'):
# i = str(i)
# if '</span>' in i or len(i) > 0:
# key, value = (i.split('</span>'))
# info[key[24:]] = value.rsplit('</li>')[0]
#for i in soup.select('.transaction li'):
# i = str(i)
# if '</span>' in i and len(i) > 0 and '抵押資訊' not in i:
# key, value = (i.split('</span>'))
# info[key[24:]] = value.rsplit('</li>')[0]
print(info)
return info
def update_to_MongoDB(one_page): # update儲存到MongoDB
if db[Mongo_TABLE].update({'鏈家編號': one_page['鏈家編號']}, {'$set': one_page}, True): #去重複
print('儲存MongoDB 成功!')
return True
return False
def pandas_to_xlsx(info): # 儲存到xlsx
pd_look = pd.DataFrame(info)
pd_look.to_excel('鏈家二手房.xlsx', sheet_name='鏈家二手房')
def writer_to_text(list): # 儲存到text
with open('lianjia.txt', 'a')as f:
f.write(json.dumps(list, ensure_ascii=False) + '\n')
f.close()
def main(url):
#time.sleep(5)
writer_to_text(open_url('http://sh.lianjia.com'+url)) #儲存到text檔案
# update_to_MongoDB(list) #儲存到Mongodb
if __name__ == '__main__':
#user_in_city = input('輸入爬取城市:')
#user_in_nub = input('輸入爬取頁數:')
Mongo_Url = 'localhost'
Mongo_DB = 'Lianjia'
Mongo_TABLE = 'Lianjia' + '\n' + str('zs')
client = pymongo.MongoClient(Mongo_Url)
db = client[Mongo_DB]
pool = Pool()
for i in generate_allurl('3', 'sh'):
writer_to_text(i)
pool.map(main, [url for url in get_allurl(i)])
相關推薦
實時爬取二手房資料
#coding=utf-8 import time import json from multiprocessing import Pool import requests from bs4 import BeautifulSoup import re impo
Scrapy爬取二手房資訊+視覺化資料分析
本篇介紹一個scrapy的實戰爬蟲專案,並對爬取資訊進行簡單的資料分析。目標是北京二手房資訊,下面開始分析。網頁結構分析採用安居客網頁資訊作為二手房的資訊來源,直接點選進入二手房資訊的頁面。每頁的住房資訊:點開連結後的詳細資訊:博主並沒有採用分割槽域進行爬取,博主是直接進行全
正則爬取二手房數據
ace req spider spi con 網址 for repl 插入數據 以下代碼是二手房的數據,代碼僅供參考,很簡單,超級簡單#encoding:utf8import requestsimport re調用網址def spider(url): html = r
用python爬取二手房交易資訊並進行分析
用python爬取二手房交易資訊並分析 第一步:編寫爬蟲 爬取某平臺上海市十個區共900條二手房的交易資訊 #爬取上海十個區的二手房價資訊 import requests from bs4 import BeautifulSoup import csv #
Selenium+Python爬取房天下二手房資料
注意!注意!注意!本文中大圖較多,建議使用PC檢視,手機端效果較差! 在上篇“Selenuim+Python網路爬蟲基礎講解”博文中講了一些Selenium的基礎知識,接下來就要開始實戰了。 其實使用Selenium爬取網頁的思路很簡單,首先梳理一下爬取流程。 開啟二手房珠海地區首頁
學習python抓取資料——鏈家北京二手房資料
最近在學習用Python進行資料分析、機器學習,基本都是用現成資料集進行模型訓練及驗證,想用一些實際資料看一下效果,於是想到用Python嘗試抓取一些實際資料。 目標:爬取鏈家網北京二手房房價、位置、面積等資料 環境:Python3.5.2,Anaconda4.2.0 1.準備工作
20180213 爬蟲爬取空氣質量資料
目標網址: 空氣質量歷史資料 1、修改爬蟲原因:網址針對爬蟲作了防範措施,直接爬取很難奏效。 2、google 的webdriver難以get內容,也許是網站針對性的進行了防範 思路: 1、利用Cenenium+PlatformJS 模擬瀏覽器請求一個頁面 2、Pandas裡
Python 利用BeautifulSoup和正則表示式 來爬取旅遊網資料
import re import requests import time from bs4 import BeautifulSoup url = ‘http://www.cntour.cn/’ r = requests.get(url) print(r.encoding,len(r.t
python 爬取了租房資料
爬取連結:https://sh.lianjia.com/zufang/ 程式碼如下: import requests # 用於解析html資料的框架 from bs4 import BeautifulSoup # 用於操作excel的框架 from xlwt import
Python3+Selenium爬取動態網頁資料
背景: 有時候想獲取網頁的資訊,然後下載裡面的圖片資料等等 遇到的問題: 有時一些網頁是動態的,一些內容是通過js非同步拉取,甚至拉取時間是懶載入的,例如滾動到元素位置的時候才載入 解決方案: 這個時候就需要Selenium神器了 Selenium 是什麼?一句話
Python 非同步爬取微博資料練習
PHP交流群:294088839, Python交流群:652376983 # js 資料爬取 from urllib.parse import urlencode import requests base_url ='https://m.weibo.cn/api/container/getI
6Python3實戰入門資料庫篇003---把爬取到的資料存到資料庫,帶資料庫去重功能
這是python3實戰入門系列的第三篇文章,要學習這一篇需要了解前兩篇,要不學起來比較費勁 python3實戰入門python爬蟲篇001---網頁爬蟲,圖片爬蟲,文章爬蟲,Python爬蟲爬取新聞網站新聞 python3操作資料庫002 藉助pycharm快速連線並操作mysql資
詳解如何批量採集58同城二手房資料及中介聯絡方式
2008年9月4日,英國《自然》雜誌刊登了一個名為“Big Data”的專輯,首次提出大資料概念,該專輯對如何研究PB級容量的大資料流,以及目前正在制訂的、用以最為充分地利用海量資料的最新策略進行了探討。2011、2012年達沃斯世界經濟論壇將大資料作為專題討論的主題之一,釋出了《大資料、大影響:國際發展新的
爬蟲例項 利用Ajax爬取微博資料
隨著代理IP技術的普及,爬蟲的使用也變得簡單起來,許多企業和個人都開始用爬蟲技術來抓取資料。那麼今天就來分享一個爬蟲例項,幫助你們更好的理解爬蟲。下面我們用程式模擬Ajax請求,將我的前10頁微博全部爬取下來。首先,定義一個方法來獲取每次請求的結果。在請求時,page是一個可變引數,所以我們將它作為方法的引數
利用python爬蟲技術動態爬取地理空間資料雲中的元資料(selenium)
python爬取地理空間資料雲selenium動態點選 爬取的網址秀一下: 爬取的資訊是什麼呢? 這個資訊的爬取涉及到右邊按鈕的點選,這屬於動態爬取的範疇,需要用到selenium 好了,那麼開始寫程式碼吧 首先匯入selenium from seleni
資料分析——廣州二手房資料分析
房價一直以來都是大多數人非常關注的物件,房價一升再升,心底一提再提。影響房價的因素有很多,但現在我們分析一下房子自身因素對價格的影響。 目標:分析廣州二手房價格的自身影響因素 資料來源:上篇文章《scrapy爬蟲爬取廣州二手房資訊》爬蟲爬取房天下網站得來的廣州二手房資料
用Python爬取微博資料生成詞雲圖片
很早之前寫過一篇怎麼利用微博資料製作詞雲圖片出來,之前的寫得不完整,而且只能使用自己的資料,現在重新整理了一下,任何的微博資料都可以製作出來,放在今天應該比較應景。 一年一度的虐汪節,是繼續蹲在角落默默吃狗糧還是主動出擊告別單身汪加入散狗糧的行列就看你啦,七夕送什麼才有心意,程式猿可以試試用
用scrapy爬取京東的資料
# -*- coding: utf-8 -*- import scrapy from ..items import JdphoneItem import sys reload(sys) sys.setdefaultencoding("utf-8") class JdSpider(scrapy.Spid
爬蟲——爬取人民網資料生成詞雲圖
1、以人民網的新聞資料為例,簡單介紹的利用python進行爬蟲,並生成詞雲圖的過程。 首先介紹python的requests庫,它就好像是一個“爬手”,負責到使用者指定的網頁上將所需要的內容爬取下來,供之後的使用。 我們可以利用python的pip功能下載requests庫,在cmd視窗輸入
Python3 爬取51job的資料存入MongoDB並分析
1.開啟51job首頁,輸入Python,地址選擇深圳,得到搜尋頁面: 3.不同點: items.py新增如下程式碼: from scrapy import Item,Field class JobsItem(Item): # define the f