使用pywikipedia來獲取頁面文本是否可能,沒有任何內部鏈接或模板&沒有圖片等?我可以使用pywikipedia獲取一個頁面的文本嗎?
1
A
回答
4
如果您的意思是「我只想獲得wikitext」,那麼請看wikipedia.Page
類和get
方法。
import wikipedia
site = wikipedia.getSite('en', 'wikipedia')
page = wikipedia.Page(site, 'Test')
print page.get() # '''Test''', '''TEST''' or '''Tester''' may refer to:
#==Science and technology==
#* [[Concept inventory]] - an assessment to reveal student thinking on a topic.
# ...
這樣您就可以從文章中獲得完整的原始wiki文本。
如果要刪除wiki語法,就像將[[Concept inventory]]
轉換爲Concept庫存等一樣,這將會更加痛苦。
這個問題的主要原因是MediaWiki wiki語法沒有定義的語法。這使得解析和剝離非常困難。我目前不知道哪種軟件可以讓你準確地做到這一點。當然有MediaWiki Parser類,但它是PHP,有點難以掌握,其目的非常不同。
但是,如果你只是想去掉鏈接,或非常簡單的wiki結構使用正則表達式:
text = re.sub('\[\[([^\]\|]*)\]\]', '\\1', 'Lorem ipsum [[dolor]] sit amet, consectetur adipiscing elit.')
print text #Lorem ipsum dolor sit amet, consectetur adipiscing elit.
,然後管道鏈接:
text = re.sub('\[\[(?:[^\]\|]*)\|([^\]\|]*)\]\]', '\\1', 'Lorem ipsum [[dolor|DOLOR]] sit amet, consectetur adipiscing elit.')
print text #Lorem ipsum DOLOR sit amet, consectetur adipiscing elit.
等。
但例如,有一個從網頁去掉嵌套模板,沒有可靠的簡便方法。對於在評論中有鏈接的圖片也是如此。這非常困難,並涉及遞歸刪除最內部的鏈接並用標記替換它並重新開始。如果需要,可以查看wikipedia.py中的templateWithParams
函數,但這不太好。
0
有一個名爲模塊,可以讓你很接近你根據你需要什麼想要什麼。它有一個名爲strip_code()的方法,它剝去了很多標記。
import pywikibot
import mwparserfromhell
test_wikipedia = pywikibot.Site('en', 'test')
text = pywikibot.Page(test_wikipedia, 'Lestat_de_Lioncourt').get()
full = mwparserfromhell.parse(text)
stripped = full.strip_code()
print full
print '*******************'
print stripped
比較片段:
{{db-foreign}}
<!-- Commented out because image was deleted: [[Image:lestat_tom_cruise.jpg|thumb|right|[[Tom Cruise]] as Lestat in the film ''[[Interview With The Vampire: The Vampire Chronicles]]''|{{deletable image-caption|1=Friday, 11 April 2008}}]] -->
[[Image:lestat.jpg|thumb|right|[[Stuart Townsend]] as Lestat in the film ''[[Queen of the Damned (film)|Queen of the Damned]]'']]
[[Image:Lestat IWTV.jpg|thumb|right|[[Tom Cruise]] as Lestat in the 1994 film ''[[Interview with the Vampire (film)|Interview with the Vampire]]'']]
'''Lestat de Lioncourt''' is a [[fictional character]] appearing in several [[novel]]s by [[Anne Rice]], including ''[[The Vampire Lestat]]''. He is a [[vampire]] and the main character in the majority of ''[[The Vampire Chronicles]]'', narrated in first person.
==Publication history==
Lestat de Lioncourt is the narrator and main character of the majority of the novels in Anne Rice's ''The Vampire Chronicles'' series. ''[[The Vampire Lestat]]'', the second book in the series, is presented as Lestat's autobiography, and follows his exploits from his youth in France to his early years as a vampire. Many of the other books in the series are also credited as being written by Lestat.
*******************
thumb|right|Stuart Townsend as Lestat in the film ''Queen of the Damned''
'''Lestat de Lioncourt''' is a fictional character appearing in several novels by Anne Rice, including ''The Vampire Lestat''. He is a vampire and the main character in the majority of ''The Vampire Chronicles'', narrated in first person.
Publication history
Lestat de Lioncourt is the narrator and main character of the majority of the novels in Anne Rice's ''The Vampire Chronicles'' series. ''The Vampire Lestat'', the second book in the series, is presented as Lestat's autobiography, and follows his exploits from his youth in France to his early years as a vampire. Many of the other books in the series are also credited as being written by Lestat.
相關問題
- 1. 我可以使用HTML頁面作爲另一個HTML頁面的內容嗎?
- 2. 我可以使用JavaScript來生成一個RSS(XML)頁面嗎?
- 3. 我可以在同一頁面上使用多個版本的jQuery嗎?
- 4. 我可以在ASP頁面中包含一個aspx頁面嗎?
- 5. 我可以從另一個腳本獲取變量的值嗎?
- 6. 可以在獲取頁面上使用SESSION vars嗎?
- 7. 我可以使用Enum引用一個文本字符串嗎?
- 8. 我可以將我自己的jsp頁面導入另一個jsp頁面嗎?
- 9. DataTemplate可以成爲一個頁面嗎?
- 10. 我們可以使用JavaScript從網頁獲取數據嗎?
- 11. 可以在一個HTML頁面上使用2個Javascript庫嗎?
- 12. 我可以禁用滾動一個ASP.net頁面嗎?
- 13. 我可以使用WordPress來爲PyPI文檔頁面供電嗎?
- 14. 我可以在GitHub頁面中使用Markdown文件嗎?
- 15. 我們可以在同一頁面中使用AJAXToolKit和Jquery嗎?
- 16. 我可以在同一個asp.net頁面中使用外部腳本和我的腳本嗎?
- 17. 我可以在SharePoint站點上使用多個主頁面嗎?
- 18. 我可以用BeautifulSoup在javascript後面獲得一個iframe嗎?
- 19. 我可以使用一個登錄頁面使用Spring 3.0 Security重定向不同的頁面嗎?
- 20. 一個CSS頁面可以引用另一個嗎?
- 21. 我可以在Asp.Net的另一頁訪問一個頁面的ViewState嗎?
- 22. javascript可以使用node.js從另一個js文件獲取一個函數嗎?
- 23. 可以使用visualforce頁面嗎?
- 24. 我可以使我的面板成爲唯一活動頁面嗎?
- 25. 頁面標題變量,我也可以從文件中獲取?
- 26. 從一個頁面獲取文本框的值到另一個頁面文本框的值
- 27. 我可以在popViewControllerAnimated上「蜷縮」一個頁面嗎?
- 28. JQuery:我可以在同一頁面有兩個選項卡嗎?
- 29. 我可以在一個腳本中使用多個函數嗎?
- 30. 我可以在同一頁面使用多個「主」html標籤嗎?
顯然我誤解了問題的範圍。鑑於沒有其他答案,我盡了最大的努力。 :-) – cdleary 2009-06-21 20:10:42