2012-10-13 7 views
1

안녕하세요, 저는 VBS로 RSS 피드를 구문 분석하고 내용을 cmd로 표시하는 방법을 알아 내려고합니다. 나는 그물 주변에서 발견 한 코드를 가지고있다. 이것은 내가 지금까지 얻은 것이다.VBS 스크립트를 통해 RSS 피드를 얻고 CMD에 내용을 표시하는 방법

배치 파일 :

:news 
start scripts\xml\getxml.exe -N --directory-prefix=%temp% http://feeds.bbci.co.uk/news/rss.xml 
:newscheck 
if NOT EXIST %temp%\rss.xml (
ping 123.45.67.89 -n 1 -w 500 > nul. 
goto newscheck 
) 
start scripts\news\parsebbcnews.vbs 
ping 123.45.67.89 -n 1 -w 500 > nul. 
:newsxmlparsecheck 
if NOT EXIST %temp%\bbcnews.txt (
ping 123.45.67.89 -n 1 -w 500 > nul. 
goto newsxmlparsecheck 
) 
set /p headline= <%temp%\bbcnews.txt 
echo %headline% 
%speech% "%headline%" 
del %temp%\rss.xml 
del %temp%\bbcnews.txt 
goto start 

다음이는 VBS를 시작합니다

Dim xmlDoc, objNodeList, plot 

Set wshShell = CreateObject("WScript.Shell") 
tfolder = wshShell.ExpandEnvironmentStrings("%TEMP%") 
Set xmlDoc = CreateObject("Msxml2.DOMDocument") 
xmlDoc.load(tfolder & "\rss.xml") 
Set objNodeList = xmlDoc.getElementsByTagName("channel/item/description") 'Node to search for 
Set objFSO = CreateObject("Scripting.FileSystemObject") 

' Write all found results into forecast.txt 
Const ForWriting = 2 
Set objTextFile = objFSO.OpenTextFile _ 
    (tfolder & "\bbcnews.txt", ForWriting, True) 
If objNodeList.length > 0 then 
For each x in objNodeList 
plot=x.Text 
objTextFile.WriteLine(plot) 
Next 'just remove this? 
objTextFile.Close 
End If 

'Extract todays data (first line) from 'forecast.txt' and write each data type to seperate line in today.txt 
Const ForReading = 1 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objTextFile = objFSO.OpenTextFile _ 
    (tfolder & "\bbcnews.txt", ForReading) 
    strNextLine = objTextFile.Readline 
    'currentsplit = Split(strNextLine , ", ") 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objTextFile = objFSO.OpenTextFile(tfolder & "\headline.txt", ForWriting, True) 
    objTextFile.WriteLine(strNextLine) 

이 영국 BBC에서 1 개 뉴스 RSS 피드를 가져옵니다. 또한 하나 이상의 피드를 표시하려고합니다.

다른 사이트에서 rss를 얻으려면 스크립트와 유사한 코드를 어떻게 얻을 수 있는지 알지 못하기 때문에이 코드를 만들지 않았습니다. 그래서 기본적으로 내가 원하는 것은 VB 스크립트로 이것을 어떻게 할 수 있고 그것을 cmd로 표시 할 수 있는지를 아는 것입니다.

답변

2

상위 헤드 라인에만 관심이있는 경우 "channel/item/description"대신 "channel/item/title"을 사용해야합니다. 많은 피드를 사용하면 설명이 HTML로 풍부 해 지므로 첫 번째 행을 인쇄하면 아무 것도 도움이되지 않습니다.

당신은 이런 식으로 두 스크립트를 단순화 수 :

cscript //NoLogo feeds.vbs 

:

Sub PrintTopHeadline(feed) 
    Set req = CreateObject("MSXML2.XMLHTTP.3.0") 
    req.Open "GET", feed, False 
    req.Send 

    Set xml = CreateObject("Msxml2.DOMDocument") 
    xml.loadXml(req.responseText) 
    WScript.StdOut.WriteLine xml.getElementsByTagName("channel/item/title")(0).Text 
End Sub 

PrintTopHeadline "http://feeds.bbci.co.uk/news/rss.xml" 
PrintTopHeadline "http://news.google.com/news?ned=us&topic=h&output=rss" 
... 

당신은하지만, cscript.exe으로 스크립트를 호출해야 할 것, 커맨드 출력을 인쇄 할 수 있도록 편집 : 피드에서 더 많은 헤드 라인을 표시하려면 PrintTopHeadline()에 루프를 추가하십시오.

For i = 0 To 4 
    WScript.StdOut.WriteLine xml.getElementsByTagName("channel/item/title")(i).Text 
Next 

HTML 태그 같은 것을 제거 할 수 있습니다 :

descr = xml.getElementsByTagName("channel/item/description")(0).Text 

Set re = New RegExp 
re.Pattern = "\s*<.+?>\s*" 
re.Global = True 

descr = Trim(re.Replace(descr, " ")) 

그러나 일반 텍스트로 다시 HTML entities를 변환하는 추가 코드가 필요 것, 예를 들면 :

이 좋은 덕분에 보이는
descr = Replace(descr, "&quot;", """") 
descr = Replace(descr, "&ntilde;", "ñ") 
... 
+0

안녕하세요. 감사합니다. 그것은 rss 피드에서 1 헤드 라인을 표시했습니다. 그러나 Im은 하나 이상의 피드를 어떻게 표시하는지 궁금해합니다. 지금은 각 피드에서 1 개의 헤드 라인 만 표시했습니다. 내가 표시 할 수 있도록 추가해야하는 것은 각 RSS 피드의 5 개의 피드 헤드 라인을 말합니다. 그리고 HTML 코드가 없어도 설명을 표시 할 수있는 방법이 있습니까? :) – Nivi

+0

내 원본 답변에 이미 2 개의 피드가 있습니다. 더 많은 헤드 라인과 HTML을 제거하려면 업데이트를 참조하십시오. –

+0

고마워요 :) 잘 했어! – Nivi

0

This page에는 도움이되는 몇 가지 정보가 있습니다. 그것은 상당히 방대한 것이므로 여기서 다시 게시하지는 않습니다.

구체적으로 그들이 만든 VBscript 클래스에 this is a link. This은 해당 클래스를 사용하는 샘플 코드입니다.

+0

, 나는이 rss 피드 http://www.rottentomatoes.com/syndication/rss/in_theaters.xml 페이지에서 데모를 시험해 보았다. 원하는 모든 것을 보여 주었다. 하지만 내가 어떻게 배치 파일에 이것을 구현하고 내 샘플 코드와 같은 명령을 사용하고 cmdfile에서 batchfile과 명령을 실행할 때 동일한 결과를 얻으려고하는지 생각해 냈습니다. 어떤 도움을 주신다면 – Nivi

+0

슬픈 링크가 더 이상 작동하지 않습니다. 나는 그들이 전체 답변을 게시하고 귀하의 참조를 인용하는 이유라고 생각합니다. – Zunair

관련 문제