Created
June 22, 2014 13:51
-
-
Save bryanyang0528/dcca5614e144ad89acde to your computer and use it in GitHub Desktop.
python 爬蟲
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## import 需要用的套件 | |
import requests | |
from BeautifulSoup import BeautifulSoup | |
import HTMLParser | |
##GET目標網站 | |
res = requests.get("http://bryannotes.blogspot.tw/") | |
##轉換成SOUP物件 | |
soup = BeautifulSoup(res.text.encode("utf-8")) | |
##開啟新檔 | |
bid_file = open("blog_links.txt",'w') | |
##抓下每篇文章的連結 | |
for link in bid_table: | |
##把前後不需要用的文字刪掉 | |
links = str([tag['href'] for tag in link.findAll('a',{'href':True})])[3:-2] | |
##分行儲存 | |
bid_file.write(links+"\n") | |
print links | |
bid_file.close() | |
bid_list = open('blog_links.txt','r') | |
##建立物件 | |
h = HTMLParser.HTMLParser() | |
##建立用來儲存的dictionary | |
blog = {} | |
##讀取網址 | |
for line in bid_list.readlines(): | |
pagelink = line.strip() | |
##一次讀取一行,GET連結內容 | |
request_get = requests.get(pagelink) | |
soup_post = BeautifulSoup(request_get.text.encode("utf-8")) | |
##抓取文章內容文字,並去除HTML跳脫字元 | |
body = h.unescape(soup_post.find("div",{'class':'post-body entry-content'}).text) | |
##抓去文章標題 | |
title = h.unescape(soup_post.find("h3",{'class':'post-title entry-title'}).text) | |
##把抓好的標題和內容文字塞回dictionary裡面 | |
blog[title] = body | |
##測試看看PO出文章標題和計算每篇文章的文字數 | |
for key in blog: | |
print key, | |
print len(blog[key]) | |
##result | |
資料分析鍊金術(七)-消費者分層-RFM模型原理與應用 572 | |
傳統市場分析人員的未來 727 | |
當行為紀錄器越來越方便和普及時 826 | |
[轉貼] 資料科學家與凡人的溝通利器:30 個把資料視覺化的簡單工具-科技報橘 227 | |
資料分析鍊金術(四)-利用八二法則建立客戶分層金字塔 851 | |
Surprise 512 | |
從反黑箱服貿協議看傳統媒體的沒落 1036 | |
自修課程推薦-Coursera-機器學習基石 (Machine Learning Foundations) 773 | |
專案管理技巧分享 546 | |
"7.5 億台幣做 Full HD 轉型,專訪 TVBS 總經理楊鳴:我們已在生死存亡關頭上"? 538 | |
不健康的社群媒體關係 1162 | |
市場調查(Marketing Research)與客戶關係管理(Customer Relationship Manage)的異同 836 | |
新年快樂 馬到成功--進入市場分析的心路歷程 535 | |
資料分析鍊金術(二)--理解、分解、再構成 859 | |
如何進入市場調查/行銷研究業!搶佔2013全球最HOT工作! 1713 | |
多工處理 575 | |
資料分析鍊金術(六)-付出越多、愛得越深-關鍵消費忠誠模型 742 | |
市場成長的動力? 905 | |
資料分析鍊金術(一)--認識手上的資料庫 1730 | |
[R][翻譯] apply、sapply、lapply之區別 2363 | |
研究方法的選擇? 596 | |
測量 475 | |
3.11日本大地震,日本YAHOO搜索關鍵字視覺化宣傳 326 | |
服務生 646 | |
資料脈絡與詮釋 936 | |
[R]最近投入了R的世界 796 | |
我所認知的BIG DATA以及企業應用 816 | |
行銷研究與市場調查 800 | |
[R]用R轉換資料結構-將矩陣型資料轉為一般資料格式 593 | |
如何設定媒體組合? 826 | |
市場調查(Marketing Research)與資料庫分析(CRM & POS DATA)對企業的幫助 701 | |
資料的切與不切,is a critical choice 508 | |
誰搞砸了好資料? 749 | |
資料分析鍊金術(五)-TAG的威力 992 | |
研究方法的選擇(二) 如何面對任性情人? 1002 | |
站在客戶的角度思考 352 | |
[R][翻譯]Reshape(transpose)! 資料的變形金剛 1316 | |
[R] TEXT MINING(文字探勘練習) 692 | |
[R]用R抓網頁資料 460 | |
資料分析鍊金術(三)--先從輪廓開始吧! 認識你的消費者! 2042 | |
[R]R的學習資源推薦 1214 | |
[翻譯]讓你的DATA更人性 (Six Ways to Make Your Data More Human) 1020 | |
[R]用R將資料依百分位數分組(Recode Data by Percentile) 連續型資料轉離散 615 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment