使用特殊导出从维基百科下载文章
我希望能够从 http://en.wikipedia 下载几千篇文章的完整历史记录。 org/wiki/Special:Export,我正在寻找一种编程方法来自动化它。我想将结果保存为 XML。
这是我的 维基百科查询。我在 Python 中开始执行以下操作,但没有得到任何有用的结果。
#!/usr/bin/python
import urllib
import codecs
f = codecs.open('workfile.xml', 'w',"utf-8" )
class AppURLopener(urllib.FancyURLopener):
version = "Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11"
urllib._urlopener = AppURLopener()
query = "http://en.wikipedia.org/w/index.php?title=Special:Export&action=submit"
data = { 'catname':'English-language_Indian_films','addcat':'', 'wpDownload':1 }
data = urllib.urlencode(data)
f = urllib.urlopen(query, data)
s = f.read()
print (s)
I want to be able to download full histories of a few thousand articles from http://en.wikipedia.org/wiki/Special:Export and I am looking for a programmatic approach to automate it. I want to save result as XML.
Here is my Wikipedia query. I started the following in Python, but that doesn't get any useful result.
#!/usr/bin/python
import urllib
import codecs
f = codecs.open('workfile.xml', 'w',"utf-8" )
class AppURLopener(urllib.FancyURLopener):
version = "Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11"
urllib._urlopener = AppURLopener()
query = "http://en.wikipedia.org/w/index.php?title=Special:Export&action=submit"
data = { 'catname':'English-language_Indian_films','addcat':'', 'wpDownload':1 }
data = urllib.urlencode(data)
f = urllib.urlopen(query, data)
s = f.read()
print (s)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
老实说,我建议使用 Mechanize 来获取页面,然后使用 lxml 或其他 xml 解析器来获取您想要的信息。通常我使用 Firefox 用户代理,因为许多程序用户代理被阻止。请注意,使用 Mechanize,您实际上可以填写表格并“单击”输入,然后“单击”导出。
I would honestly suggest using Mechanize to get the page, then using lxml or another xml parser to get the information you want. Usually I use the firefox user-agent as many program user-agents are blocked. Note that with Mechanize you can actually fill out the form and "click" enter, then "click" export.