首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python-Wikipedia自动下载器

Python-Wikipedia自动下载器
EN

Stack Overflow用户
提问于 2011-03-12 05:20:47
回答 3查看 622关注 0票数 0

使用Python3.1,有没有人知道如何让Python3应用程序允许用户编写一个文本文件,其中多个单词之间用逗号分隔。该程序应读取该文件,并下载所请求项目的维基百科页面。例如,如果他们输入hello,http://www.wikip-3,chicken,它就会进入维基百科,下载http://www.wikipedia.com/wiki/hello,python……有人认为他们能做到吗?

当我说“下载”的时候,我指的是下载文本,而不是图片。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-12 19:21:47

查查urllib.request吧。

票数 1
EN

Stack Overflow用户

发布于 2011-03-12 17:46:15

你准确地描述了如何制作这样一个程序。那么问题是什么呢?

您读取该文件,使用逗号拆分,然后下载URL。完成了!

票数 1
EN

Stack Overflow用户

发布于 2012-08-04 06:05:01

检查下面的代码,它下载html,没有图像,但您可以从正在解析的xml文件中访问它们,以获得url。

代码语言:javascript
复制
from time import sleep
import urllib
import urllib2
from xml.dom import minidom, Node

def main():
    print "Hello World"

    keywords = []

    key_file = open("example.txt", 'r')
    if key_file:
        temp_lines = key_file.readlines()

        for keyword_line in temp_lines:
            keywords.append(keyword_line.rstrip("\n"))

        key_file.close()

    print "Total keywords: %d" % len(keywords)
    for keyword in keywords:
        url = "http://en.wikipedia.org/w/api.php?format=xml&action=opensearch&search=" + keyword
        xmldoc = minidom.parse(urllib.urlopen(url))
        root_node = xmldoc.childNodes[0]

        section_node = None
        for node in root_node.childNodes:
            if node.nodeType == Node.ELEMENT_NODE and \
            node.nodeName == "Section":
                section_node = node
                break

        if section_node is not None:
            items = []
            for node in section_node.childNodes:
                if node.nodeType == Node.ELEMENT_NODE and \
                node.nodeName == "Item":
                    items.append(node)

            if len(items) == 0:
                print "NO results found"
            else:
                print "\nResults found for " + keyword + ":\n"
                for item in items:
                    for node in item.childNodes:
                        if node.nodeType == Node.ELEMENT_NODE and \
                        node.nodeName == "Text":
                            if len(node.childNodes) == 1:
                                print node.childNodes[0].data.encode('utf-8')

                file_name = None
                for node in items[0].childNodes:
                    if node.nodeType == Node.ELEMENT_NODE and \
                    node.nodeName == "Text":
                        if len(node.childNodes) == 1:
                            file_name = "Html\%s.html" % node.childNodes[0].data.encode('utf-8')
                            break

                if file_name is not None:
                    file = open(file_name, 'w')
                    if file:
                        for node in items[0].childNodes:
                            if node.nodeType == Node.ELEMENT_NODE and \
                            node.nodeName == "Url":
                                if len(node.childNodes) == 1:
                                    user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)'
                                    header = { 'User-Agent' : user_agent }
                                    request = urllib2.Request(url=node.childNodes[0].data, headers=header)
                                    file.write(urllib2.urlopen(request).read())
                                    file.close()
                                    break


    print "Sleeping"
    sleep(2)

if __name__ == "__main__":
    main()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5278513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档