2013-11-03 24 views
-1

我想使用R或任何其他類似軟件做類似this SO question的操作。R爲內容和標題中的關鍵字刮博客

的附加要求我想要的是:

  1. 刮在博文內容關鍵字
  2. 並計算它們的出現

爲了簡單起見,我們假設有問題的博客也是www.jamesaltucher.com

通過關鍵詞,我的意思是任何單詞是通常的單詞谷歌忽略像「的」,「一」,「一」,「的」等。

請假設博客是公開的,我沒有管理員帳戶。

按照內容,我的意思是博客文章內的內容。不一定是評論。

UPDATE: 目前,我可以提取www.jamesaltucher.com/sitemap.xml

library(XML) 
library(RCurl) 
url.link <- 'http://www.jamesaltucher.com/sitemap.xml' 
blog <- getURL(url.link) 
blog   <- htmlParse(blog, encoding = "UTF-8") 
titles <- xpathSApply (blog ,"//loc",xmlValue)    ## titles 

所有博客帖子的網址都截圖。

enter image description here

下一步是通過所有的條目titles迭代並提取關鍵字頻率計數的內容。

我該怎麼做呢?

+0

因此,發佈一些代碼與您選擇的測試網站的目標。 –

回答

0

您可以在Python很容易地做到這一點,也許你在R中可以使用RPY或

scan(pipe("python scraper.py | cat")) 

最困難的部分,必須正確得到博客的內容結合下面的Python腳本...如果你簡單地做soup.text。你可能會得到一些博客的源代碼,而不是真正的內容。您需要使用HTML標籤,ID,類別等來正確定位您的內容。等等。

欲瞭解更多信息,請參閱BeautifulSoup Search the tree的此頁。

import urllib2 
from collections import Counter 
from bs4 import BeautifulSoup 

# get the html page of your target blog 
soup = BeautifulSoup(urllib2.urlopen('http://datafireball.com/')) 
# first, you need to retrieve the content of the blog 
content = soup.find("div", {"id":"content"}).text.encode('utf-8') 
# second, you count the frequency 
cnt = Counter(content.split()) 
# print out the result to verify it works 
print cnt 

輸出看起來是這樣的:

Counter({'the': 73, 'to': 41, 'of': 34, 'is': 28, 'a': 27, 'R': 27, 'in': 24, 'you': 21, 'can': 17, 'and': 15, 'that': 13, 'it': 11, '<-': 10, 'read': 10, 'all': 9, 'data': 9, 'how': 9, 'will': 9, '$': 9, 'by': 9, 'into': 9, 'this': 8, 'on': 8, 'line': 8, 'with': 8, '1': 7, 'not': 7, 'which': 7, 'I': 7, 'model': 7, 'for': 7, 'install': 6, 'linear': 6, 'standard': 6, '>': 6, 'You': 6, 'be': 6, 'your': 6, ':': 6, 'Redhat': 5, '.entry-header': 5, 'use': 5, 'chr': 5, '.entry-meta': 5, '2013': 5, 'need': 5, 'note': 5, 'The': 5, 'do': 5, 'Leave': 5, 'datafireball': 5, '.entry-content': 5, '#post-##': 5, 'Python': 5, 'or': 5, 'but': 5, '\xe2\x80\x93': 5, 'row': 4, 'R-squared': 4, 'num': 4, 'are': 4, 'from': 4, 'October': 4, 'List': 4, 'then': 4, 'our': 4, '2': 4, '=': 4, 'find': 4, 'make': 4, 'command': 4, 'In': 4, 'R,': 4, 'some': 4, 'To': 4, 'file': 4, 'code': 3, 'very': 3, 'what': 3, '3.': 3, 'working': 3, 'write': 3, 'help': 3, 'fit': 3, 'actually': 3, '2.': 3, 'R.': 3, 'out': 3, 'squared': 3, 'x': 3, 'first': 3, 'script': 3, '1.': 3, 'Then': 3, '3': 3, 'import': 3, 'we': 3, '~': 3, 'parse': 3, 'result': 3, 'really': 3, 'page': 3, 'package': 3, 'there': 3, 'up': 3, 'an': 3, 'kinds': 3, 'As': 3, 'code,': 3, '-c': 2, '-y': 2, 'string': 2, 'every': 2, '"a"': 2, 'Python.': 2, 'current': 2, '10,': 2, 'here': 2, 'change': 2, 'makes': 2, 'few': 2, '6': 2, 'tell': 2, '98': 2, 'flag': 2, 'my': 2, 'indicates': 2, 'Coefficient': 2, '0.9693628': 2, 'product': 2, '"B"': 2, 'su': 2, 'What': 2, 'R-squared:': 2, 'Here': 2, 'python': 2, 'easily': 2, 'all,': 2, 'From': 2, 'print': 2, 'created': 2, 'could': 2, '*': 2, 'white': 2, 'x,': 2, 'kind': 2, 'seamlessly': 2, 'Package': 2, 'points': 2, 'Hadoop': 2, 'Note,': 2, 'one': 2, 'title': 2, 'only': 2, 'pretty': 2, 'get': 2, 'see': 2, 'closed': 2, 'connection.': 2, 'BeautifulSoup': 2, 'much': 2, 'look': 2, 'while': 2, 'if': 2, 'connection': 2, 'check': 2, 'python.exec(\xe2\x80\x9cprint': 2, 'uses': 2, 'datafireball.com': 2, 'well': 2, 'y': 2, 'If': 2, 'just': 2, 'However,': 2, 'directory,': 2, 'input': 2, 'using': 2, 'like': 2, '[1]': 2, 'back': 2, '/': 2, 'noise': 2, 'run': 2, 'power': 2, 'anything': 2, 'trying': 2, 'called': 2, 'as': 2, 'when': 2, '4.': 2, 'together': 2, 'at': 2, 'sometimes,': 1, 'Web': 1, 'existing': 1, 'arguments,': 1, 'BeautifulSoup(stream)': 1, 'Take': 1, '(which': 1, '[yes/no]\xe2\x80\x98': 1, 'whose': 1, 'calculate': 1, 'default,': 1, "{\xe2\x80\x98class\xe2\x80\x99:'site-description\xe2\x80\x99}).text.encode(\xe2\x80\x98utf-8\xe2\x80\xb2)": 1, 'Residual': 1, 'str_py_dict': 1, 'languages': 1, 'asking': 1, 'method=\xe2\x80\x9dpearson\xe2\x80\x9d)': 1, 'otherwise,': 1, '***': 1, 'level': 1, 'list': 1, 'leave': 1, 'guy': 1, 'refer': 1, 'Rscript!': 1, 'lm(formula': 1, 'Functions': 1, 'Stats': 1, '\xe2\x80\x98**\xe2\x80\x99': 1, 'else,': 1, 'data(iris)': 1, '3Q': 1, '27,': 1, 'range.': 1, 'object.': 1, '"A"': 1, '"(1,\'a\',': 1, 'R-core-devel': 1, 'Determination.)': 1, '+': 1, '4': 1, 'version': 1, 'tips': 1, 'satisfy': 1, '10)': 1, 'Combining': 1, "input<-file('stdin',": 1, 'datafireball.py': 1, 'objects': 1, 'python.load(\xe2\x80\x98/tmp/datafireball.py\xe2\x80\x99)': 1, 'write(result,': 1, '(JSON),': 1, 'Never\xc2\xa0Use': 1, 'error:': 1, '"[1,\'a\',': 1, 'commonly': 1, 'here,': 1, '{': 1, 'Pythonic': 1, 'divide': 1, "'rpm": 1, 'Correlation': 1, 'type.': 1, 'sys': 1, 'output,': 1, 'apply': 1, 'unit': 1, 'figure': 1, '0': 1, 'edit(data)': 1, '/user/bin/R': 1, 'going': 1, 'm1': 1, '\xe2\x80\x9cshebang\xe2\x80\x9d': 1, '0.9691': 1, '0.001': 1, 'flat': 1, 'started': 1, 'freedom': 1, 'line.': 1, 'screen.': 1, 'So': 1, 'work': 1, 'Actually,': 1, 'soup': 1, '-0.6669': 1, 'py_iris.keys()\xe2\x80\x9d)': 1, '0.9694,': 1, 'stream': 1, 'Min': 1, '0.1': 1, '<2e-16': 1, 'something': 1, 'want': 1, 'Signif.': 1, 'Coefficients:': 1, 'r^2': 1, 'means': 1, 'Also,': 1, 'utilize': 1, 'command"),': 1, 'parameter': 1, 'len(py_iris)\xe2\x80\x9d)': 1, 'information': 1, '(Move': 1, '\xe2\x80\x9cR\xe2\x80\x9d': 1, 'processed.': 1, '(References:\xc2\xa0Pearson': 1, 'lines': 1, 'happen.': 1, 'string/stdout()': 1, 'so': 1, 'correlation': 1, 'again.': 1, 'sd': 1, 'lm(y': 1, 'record.': 1, 'already': 1, 'through': 1, 'looks': 1, 'defination):': 1, 'type=\xe2\x80\x9do\xe2\x80\x9d,': 1, 'mentioned': 1, 'interpreter': 1, 'still': 1, 'set.seed(100)': 1, 'its': 1, 'readLines:': 1, 'Multiple': 1, '-Uvh': 1, 'degrees': 1, '0.0003613\xc2\xa0,': 1, '2,': 1, '<': 1, 'good': 1, 'computer\xe2\x80\x99s': 1, 'introduce': 1, 'they': 1, "u'Sepal.Width',": 1, 'cluster.': 1, 'name': 1, '"")': 1, 'fully': 1, 'above(sd=5).': 1, 'mode': 1, 'each': 1, 'found': 1, 'side': 1, 'soup.find(\xe2\x80\x98div\xe2\x80\x99,': 1, '(Pearson\xe2\x80\x99s': 1, 'doing': 1, 'series': 1, 'idea': 1, '\xe2\x80\x93\xc2\xa0stdin/stdout': 1, 'Which': 1, 'happen': 1, 'python.load()': 1, 'is:': 1, 'df': 1, 'Std.': 1, 'Residuals:': 1, 'matrix': 1, 'Very': 1, 'content': 1, 'time.': 1, 'install.': 1, 'got': 1, 'file()/file_test()/file.access()/file.remove()/file.copy()/file.exist()/file.info()/file.append()/file.symlink()/file.link()/file.path()/file.show()': 1, 'written': 1, 'This': 1, 'python.assign(PyObject,RObject)': 1, 'gap.': 1, 'base': 1, '$cat': 1, 'put': 1, 'testData': 1, 'python.exec("python': 1, 'definition': 1, 'str(python.get(str_py_tuple))': 1, 'length': 1, 'yum': 1, '\'B\']"': 1, 'go:': 1, 'stdout:': 1, '1Q': 1, 'There': 1, 'yourself': 1, '6.': 1, 'input\xe2\x80\x99': 1, 'says': 1, 'open': 1, 'RGui': 1, 'IDE': 1, 'plot(y': 1, '0.01': 1, 'system': 1, '0.05': 1, 'their': 1, 'Rstudio.': 1, 'perfectly': 1, 'Data': 1, '#!/usr/bin/Rscript': 1, 'gives': 1, 'x)': 1, 'str(python.get(str_py_list))': 1, 'Install': 1, 'Enterprise': 1, 'easier.': 1, 'from?': 1, "http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm'": 1, '10': 1, 'translate': 1, '1:': 1, 'r': 1, 'df)': 1, '\xe2\x80\x9cstdin\xe2\x80\x9d': 1, '\xe2\x80\x98standard': 1, 'stdin': 1, 'cases,': 1, 'bs4': 1, 'turned': 1, '-0.2093': 1, 'have': 1, 'cleaned': 1, 'Service),': 1, 'tells': 1, 'F-statistic:': 1, '-': 1, 'end,': 1, 'build': 1, 'deviation.': 1, 'order.': 1, 'sure': 1, 'node': 1, 'data.frame': 1, '55.684': 1, 'sum(m1$residuals^2)': 1, 'most': 1, 'curve.': 1, 'connected': 1, 'RPython': 1, 'see,': 1, 'extremely': 1, 'str_py_list': 1, 'request': 1, 'pipe': 1, 'col=\xe2\x80\x9dred\xe2\x80\x9d)': 1, '}': 1, '1.37592': 1, '1:100': 1, 'slope': 1, 'returned.': 1, '2.06135': 1, 'n=1)': 1, 'pride,': 1, 'Manager)': 1, 'abline(m1,': 1, "'yum": 1, 'sudo': 1, 'y,': 1, 'Pr(>|t|)': 1, 'list).': 1, '\xe2\x80\x98r\xe2\x80\x99': 1, 'x))': 1, 'Windows).': 1, 'Base': 1, 'readLines(input,': 1, 'main=\xe2\x80\x9dy': 1, 'beat': 1, 'str(python.get(str_py_dict))': 1, 'codes:': 1, 'cannot': 1, 'during': 1, 'description': 1, '0.506': 1, 'Linux).': 1, 'summary': 1, 'bad': 1, '6.4': 1, 'them.': 1, '\xe2\x80\x98Is': 1, 'where': 1, 'inside': 1, 'xinch': 1, 'package,': 1, 'decided': 1, '0.03544': 1, 'close': 1, 'into\xc2\xa0R': 1, '#': 1, 'Again,': 1, 'please': 1, 'opened': 1, 'pump': 1, 'determination(\xe5\x86\xb3\xe5\xae\x9a\xe7\xb3\xbb\xe6\x95\xb0),': 1, 'across': 1, '0,': 1, 'available': 1, 'sd=10\xe2\x80\xb3)': 1, 'R-dev..': 1, 'connection,': 1, 'answers': 1, 'interface': 1, 'packages': 1, 'come': 1, 'EPEL(Extra': 1, '26.1505': 1, 'conlusion,': 1, 'R),': 1, 'cov(x,': 1, 'way,': 1, 'delete': 1, 'comes': 1, 'library(\xe2\x80\x98rPython\xe2\x80\x99)': 1, '1+1}"': 1, 'point': 1, 'dataframe': 1, 'file=\xe2\x80\x9ddata\xe2\x80\x9d\xe2\x80\xa6),': 1, 'ETL': 1, 'there,': 1, 'R-core': 1, 'we\xe2\x80\x99ve': 1, "u'Petal.Width',": 1, 'basic': 1, 'line..': 1, 'empty': 1, 'life': 1, 'options.': 1, 'covariance': 1, 'posts,': 1, 'catch': 1, 'Estimate': 1, 'And': 1, 'Adjusted': 1, 'edit/vi/emacs/xemacs/xedit': 1, '1.97333': 1, 'summary(m1': 1, 'python.get': 1, 'value': 1, 'versa.': 1, 'write(x,': 1, 'loop': 1, 'Rscript': 1, 'Above': 1, 'n=1': 1, 'cluster': 1, 'rnorm(length(x),': 1, '1000,': 1, 'regression': 1, 'different': 1, 'flag.': 1, '(sd(x)*sd(y))': 1, '2.2e-16': 1, 'html': 1, 'inch': 1, 'mode,': 1, 'document': 1, 'used': 1, 'http': 1, '\xe2\x80\x98*\xe2\x80\x99': 1, 'moment': 1, 'datatypes': 1, 'Coefficient,': 1, 'object': 1, 'this(Wikipedia': 1, 'getwd()/write()/unlink()': 1, 'str_py_tuple': 1, '10.23': 1, 'sys.path.append(\xe2\x80\x98/Library/Python/2.7/site-packages/beautifulsoup4-4.2.1-py2.7.egg\xe2\x80\x99)': 1, 'squared\xe2\x80\x9d': 1, 'distribute': 1, '(record)': 1, 'computing.': 1, 'stored': 1, 'questions': 1, 'rule..': 1, 'yes': 1, 'case,': 1, 'C-level': 1, 'console': 1, 'combine': 1, 'easy': 1, '-22.6284': 1, '\xc2\xa0You': 1, "'r')": 1, 'smart': 1, 'Pearson': 1, '6.1186': 1, 'format': 1, 'know': 1, 'helpful': 1, 'OK': 1, '\xe2\x80\x9cR\xc2\xa0squared\xe2\x80\x9d': 1, 'Streaming': 1, '5.': 1, 't': 1, 'popular': 1, 'output': 1, 'yinch': 1, 'simple,': 1, 'often': 1, 'people': 1, 'successfully': 1, 'library': 1, 'Call:': 1, '"{1:2,': 1, 'duration': 1, "[u'Petal.Length',": 1, 'users': 1, 'happens': 1, 'stdin/stdout': 1, 'does': 1, 'urllib2.urlopen(\xe2\x80\x98http://datafireball.com/&#8217;)': 1, 'either': 1, 'process': 1, 'while(length(row)>0)': 1, 'post': 1, 'super': 1, "'a':'A',": 1, 'First': 1, 'here(clearly,': 1, '\xe2\x80\x98.\xe2\x80\x99': 1, 'comment,': 1, 'important': 1, 'B:': 1, 'RPM(Redhat': 1, 'Use': 1, 'own': 1, '0.667': 1, 'Bash,': 1, 'file:': 1, 'coefficient': 1, 'dataset': 1, 'determine': 1, 'python.assign(\xe2\x80\x98py_iris\xe2\x80\x99,': 1, '\xe2\x80\x9cR': 1, 'R-core,': 1, 'a:': 1, 'computer': 1, 'datasets.': 1, 'DF,': 1, 'question': 1, 'start': 1, '\xe2\x80\x9crt\xe2\x80\x9d': 1, 'low': 1, "'B':": 1, 'call': 1, 'November': 1, 'function': 1, 'urllib2,': 1, 'complete': 1, 'enough': 1, 'properly': 1, 'Error': 1, 'p-value:': 1, 'open,': 1, 'sum((y-mean(y))^2)': 1, 'embedded': 1, '\'B\')"': 1, 'functions,': 1, 'below': 1, '(Intercept)': 1, '0.9694': 1, 'on\xc2\xa0Redhat6': 1, 'organize': 1, 'Usually,': 1, 'distribution': 1, '(Amazon': 1, 'etc.': 1, 'plotting)': 1, 'interactive': 1, 'Squared': 1, 'single': 1, 'model.': 1, "u'Sepal.Length',": 1, '15,': 1, 'fill': 1, 'machine': 1, 'functions': 1, 'read.csv\xe2\x80\xa6etc.': 1, 'output.': 1, 'Read': 1, 'Max': 1, '3101': 1, "R-devel'": 1, '5': 1, 'nonsense\xe2\x80\xa6': 1, 'small.': 1, 'deviation': 1, "u'Species']": 1, 'Streaming.': 1, 'Median': 1, 'urllib2': 1, 'journey': 1, '\xe2\x80\xa6': 1, '-6.8113': 1, 'debian': 1, 'position.': 1, '\xe2\x80\x94': 1, 'vice': 1, '\xe2\x80\x99': 1, '\xe2\x80\x98': 1, '\xe2\x80\x98***\xe2\x80\x99': 1}) 
+0

爲什麼你需要管到'貓'? –

0

在R,這樣的事情會爲你工作:

out <- lapply(titles, function(url) { 
    content <- getURL(url) 
    content <- gsub("<.*?>", "",content) 
    content <- gsub("[[:space:][:punct:][:digit:]]+", " ",content) 
    content <- tolower(content) 
    table(strsplit(content,' ')) 
}) 

結果是表的列表,但你可以有它只是直接返回strsplit的輸出。

您可能需要檢查並移除不喜歡的字詞(例如,if,the等),但要做到這一點,您需要一個完整的字典來刪除這些字詞。

相關問題