
傳送門:
- Python數(shù)據(jù)科學(xué)(一)- python與數(shù)據(jù)科學(xué)應(yīng)用(Ⅰ)
- Python數(shù)據(jù)科學(xué)(二)- python與數(shù)據(jù)科學(xué)應(yīng)用(Ⅱ)
- Python數(shù)據(jù)科學(xué)(三)- python與數(shù)據(jù)科學(xué)應(yīng)用(Ⅲ)
- Python數(shù)據(jù)科學(xué)(四)- 數(shù)據(jù)收集系列
- Python數(shù)據(jù)科學(xué)(五)- 數(shù)據(jù)處理和數(shù)據(jù)采集
- Python數(shù)據(jù)科學(xué)(六)- 資料清理(Ⅰ)
- Python數(shù)據(jù)科學(xué)(七)- 資料清理(Ⅱ)
- Python數(shù)據(jù)科學(xué)(八)- 資料探索與資料視覺化
- Python數(shù)據(jù)科學(xué)(九)- 使用Pandas繪制統(tǒng)計圖表
最近因為工作的事比較忙,要學(xué)的東西也很多,沒有及時更新,下一階段我會盡力一天一更的,一塊學(xué)習(xí)的朋友跟緊不走丟ヽ(ˋ▽ˊ)ノ
每時每刻,搜索引擎和網(wǎng)站都在采集大量信息,非原創(chuàng)即采集。采集信息用的程序一般被稱為網(wǎng)絡(luò)爬蟲(Web crawler)、網(wǎng)絡(luò)蜘蛛(Web spider),其行為一般是先“爬”到對應(yīng)的網(wǎng)頁上,再把需要的信息“鏟”下來。說的通俗易懂一點網(wǎng)絡(luò)數(shù)據(jù)采集程序也像是一只辛勤采蜜的小蜜蜂,它飛到花(目標網(wǎng)頁)上,采集花粉(需要的信息),經(jīng)過處理(數(shù)據(jù)清洗、存儲)變成蜂蜜(可用的數(shù)據(jù))。
1.處理不同格式的數(shù)據(jù)
網(wǎng)絡(luò)數(shù)據(jù)采集大有所為。在大數(shù)據(jù)深入人心的時代,網(wǎng)絡(luò)數(shù)據(jù)采集作為網(wǎng)絡(luò)、數(shù)據(jù)庫與機器學(xué)習(xí)等領(lǐng)域的交匯點,已經(jīng)成為滿足個性化網(wǎng)絡(luò)數(shù)據(jù)需求的最佳實踐。搜索引擎可以滿足人們對數(shù)據(jù)的共性需求,即“所見即所得”,而網(wǎng)絡(luò)數(shù)據(jù)采集技術(shù)可以進一步精煉數(shù)據(jù),把網(wǎng)絡(luò)中雜亂無章的數(shù)據(jù)聚合成合理規(guī)范的形式,方便分析與挖掘,真正實現(xiàn)“通過數(shù)據(jù)進行分析”。工作中,你可能經(jīng)常為找數(shù)據(jù)而煩惱,或者眼睜睜看著眼前的幾百頁數(shù)據(jù)卻只能長恨咫尺天涯,又或者數(shù)據(jù)雜亂無章的網(wǎng)站中滿是帶有陷阱的表單和坑爹的驗證碼,甚至需要的數(shù)據(jù)都在網(wǎng)頁版的 PDF 和網(wǎng)絡(luò)圖片中。而作為一名反爬蟲工程師,你也需要了解常用的網(wǎng)絡(luò)數(shù)據(jù)采集手段,以及常用的網(wǎng)絡(luò)表單安全措施,以提高網(wǎng)站訪問的安全性,所謂道高一尺,魔高一丈...(所以對于爬蟲工程師來說每天都是不停地和對方的反爬工程師斗智斗勇,這個改天再嘮...)
扯得有點遠 ,我們言歸正傳,網(wǎng)絡(luò)數(shù)據(jù)采集之前我們先了解一下怎么對不同格式的數(shù)據(jù)進行處理...
1.處理CSV格式數(shù)據(jù)
1.下載數(shù)據(jù)
數(shù)據(jù)來源:http://data.stats.gov.cn/easyquery.htm?cn=C01

2.處理數(shù)據(jù)


注意:處理Excel格式、Json格式數(shù)據(jù)數(shù)據(jù)也類似,分別使用Pandas中的read_excel()方法和read_json()方法。
3.處理XML格式數(shù)據(jù)

2.網(wǎng)絡(luò)爬蟲
這部分由于之前寫過,這里就不再進行詳細寫了,可以參考往期文章。
- Python網(wǎng)絡(luò)爬蟲(一)- 入門基礎(chǔ)
- Python網(wǎng)絡(luò)爬蟲(二)- urllib爬蟲案例
- Python網(wǎng)絡(luò)爬蟲(三)- 爬蟲進階
- Python網(wǎng)絡(luò)爬蟲(四)- XPath
- Python網(wǎng)絡(luò)爬蟲(五)- Requests和Beautiful Soup
- Python網(wǎng)絡(luò)爬蟲(六)- Scrapy框架
- Python網(wǎng)絡(luò)爬蟲(七)- 深度爬蟲CrawlSpider
- Python網(wǎng)絡(luò)爬蟲(八) - 利用有道詞典實現(xiàn)一個簡單翻譯程序
- 利用簡書首頁文章標題數(shù)據(jù)生成詞云
- Spider與OpenPyXL的結(jié)合
- 爬取拉勾網(wǎng)招聘信息并使用xlwt存入Excel
- Python可以做哪些好玩的事之自動刷票
- Selenium與PhantomJS
- 使用Selenium抓取QQ空間好友說說
- Selenium 的使用
3.小試牛刀
說了那么多理論性的東西,接下來就開始步入正軌了。

1.獲取騰訊新聞首頁新聞標題及鏈接,并以Excel形式存儲

import requests
import pandas
from bs4 import BeautifulSoup
res = requests.get('https://news.qq.com/') # 數(shù)據(jù)采集目標地址
soup = BeautifulSoup(res.text, 'html.parser') # 解析網(wǎng)頁
newsary = [] # 定義空列表
for news in soup.select('.Q-tpWrap .text'):
newsary.append({'title': news.select('a')[0].text,
'url':news.select('a')[0]['href']}) # 分別獲取超鏈接中文本信息和href屬性,即地址
newdf = pandas.DataFrame(newsary) # 創(chuàng)建一個DataFrame
newsdf.to_excel('news.xlsx') # 輸出到excel表格
print(newsary[0])

2.抓取房天下房價信息并存儲


import requests
import pandas as pd
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
ua_list = UserAgent() # 設(shè)置user-agent列表,每次請求時,隨機挑選一個user-agent
my_headers = {
'user-agent': ua_list.random
}
# 獲取所有的url
def get_url():
num = 1
sum_url = []
while num < 101:
usual_url = 'http://esf.sh.fang.com/house/i3'
home_url = usual_url + str(num)
print(home_url)
res = requests.get(url=home_url, headers=my_headers)
num+=1
soup = BeautifulSoup(res.text, 'html.parser')
domain = 'http://esf.sh.fang.com'
for house in soup.select('.houseList dl'):
try:
# title = house.select('.title')[0].text.strip() # 清除多余的換行
url1 = domain + house.select('.title a')[0]['href']
sum_url.append(url1)
except Exception as e:
print(e)
print(len(sum_url))
return sum_url
def houseary():
houseary_url = get_url()
houseary = []
for url in houseary_url:
print(url)
content = requests.get(url=url, headers=my_headers)
soup1 = BeautifulSoup(content.text, 'html.parser')
try:
info = {}
info['標題'] = soup1.select('.title')[0].text.strip()
info['總價'] = soup1.select('.trl-item')[0].text
info['戶型'] = soup1.select('.tt')[0].text.strip()
info['建筑面積'] = soup1.select('.tt')[1].text
info['單價'] = soup1.select('.tt')[2].text
info['朝向'] = soup1.select('.tt')[3].text
info['樓層'] = soup1.select('.tt')[4].text
info['裝修'] = soup1.select('.tt')[5].text
info['小區(qū)'] = soup1.select('.rcont')[0].text.strip().replace('\n', '')
info['區(qū)域'] = soup1.select('.rcont')[1].text.replace('\n', '').replace('\r', '').replace(' ', '')
info['經(jīng)紀人'] = soup1.select('.pn')[0].text
info['聯(lián)系電話'] = soup1.select('.pnum')[0].text
houseary.append(info)
except Exception as e:
print(e)
print(houseary)
print(len(houseary))
df = pd.DataFrame(houseary)
df.to_excel('house.xlsx')
if __name__ == '__main__':
houseary()
后臺運行程序,經(jīng)過半個小時的戰(zhàn)績,總算把數(shù)據(jù)爬下來了,這個效率我覺得是時候?qū)W一波分布式爬蟲了...

拿到了數(shù)據(jù),我們就該做數(shù)據(jù)的清理了,下一階段數(shù)據(jù)的清理、資料探索與資料視覺化...