import requests
from BeautifulSoup import BeautifulSoup
import HTMLParser
res = requests.get("http://bryannotes.blogspot.tw/")
soup = BeautifulSoup(res.text.encode("utf-8"))
bid_table = soup.findAll('h3',{'class':'post-title entry-title'})
print bid_table[1].findAll('a',{'href':True})
[<a href="http://bryannotes.blogspot.tw/2014/06/python.html">[Python] 基本語法介紹、教學與簡單範例</a>]
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()
http://bryannotes.blogspot.tw/2014/06/python-list-dictionary.html http://bryannotes.blogspot.tw/2014/06/python.html http://bryannotes.blogspot.tw/2014/06/python-python.html http://bryannotes.blogspot.tw/2014/06/python-crawler-blog.html http://bryannotes.blogspot.tw/2014/06/rreshapetranspose.html http://bryannotes.blogspot.tw/2014/06/data-six-ways-to-make-your-data-more.html http://bryannotes.blogspot.tw/2014/06/r-applysapplylapply.html http://bryannotes.blogspot.tw/2014/06/30.html http://bryannotes.blogspot.tw/2014/06/blog-post_11.html http://bryannotes.blogspot.tw/2014/06/blog-post_5007.html http://bryannotes.blogspot.tw/2014/06/r-text-mining.html http://bryannotes.blogspot.tw/2014/06/blog-post.html http://bryannotes.blogspot.tw/2014/06/rrecode-data-by-percentile.html http://bryannotes.blogspot.tw/2014/05/r_15.html http://bryannotes.blogspot.tw/2014/05/r_8.html http://bryannotes.blogspot.tw/2014/05/2013hot.html http://bryannotes.blogspot.tw/2014/05/r.html http://bryannotes.blogspot.tw/2014/04/blog-post.html http://bryannotes.blogspot.tw/2014/04/r.html http://bryannotes.blogspot.tw/2014/03/blog-post_19.html
bid_list = open('blog_links.txt','r')
h = HTMLParser.HTMLParser()
blog = {}
for line in bid_list.readlines():
pagelink = line.strip()
request_get = requests.get(pagelink)
soup_post = BeautifulSoup(request_get.text.encode("utf-8"))
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)
blog[title] = body
for key in blog:
print key,
print len(blog[key])
從反黑箱服貿協議看傳統媒體的沒落 1036 [Python] 基礎篇:流程控制、物件與方法、List & Dictionary 285 [R][翻譯] apply、sapply、lapply之區別 2363 傳統市場分析人員的未來 727 [Python] python入門使用心得 692 [R][翻譯]Reshape(transpose)! 資料的變形金剛 1316 資料脈絡與詮釋 936 [轉貼] 資料科學家與凡人的溝通利器:30 個把資料視覺化的簡單工具-科技報橘 227 資料的切與不切,is a critical choice 508 [R]用R轉換資料結構-將矩陣型資料轉為一般資料格式 593 [Python] 現學現賣之網路爬蟲(Crawler)--以抓本BLOG為例 465 如何進入市場調查/行銷研究業!搶佔2013全球最HOT工作! 1713 [R]用R將資料依百分位數分組(Recode Data by Percentile) 連續型資料轉離散 615 [R]用R抓網頁資料 460 [R]最近投入了R的世界 796 [R] TEXT MINING(文字探勘練習) 692 [R]R的學習資源推薦 1214 [翻譯]讓你的DATA更人性 (Six Ways to Make Your Data More Human) 1020 專案管理技巧分享 546 [Python] 基本語法介紹、教學與簡單範例 289
# coding=UTF-8
f = open("C:\\blog_text.txt","w")
for key in blog:
f.write(key.encode('utf-8')+",")
f.write(blog[key].encode('utf-8')+"\n")
f.close()