Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使一个简单的网站复制应用程序面向对象?_Python_Oop_Design Patterns_Object Oriented Analysis - Fatal编程技术网

Python 如何使一个简单的网站复制应用程序面向对象?

Python 如何使一个简单的网站复制应用程序面向对象?,python,oop,design-patterns,object-oriented-analysis,Python,Oop,Design Patterns,Object Oriented Analysis,直截了当地说,我是面向对象编程的新手,在学习Python时,我编写了一个简单的过程程序,它接受URL并将数据从URL提取到文本文件 代码看起来像这样, 忽略变量,这是虚构的 def open_URL(url): # try the URL # except errors def fetch_raw_content(url): # fetch the raw content from web # return raw_content def clean_up_r

直截了当地说,我是面向对象编程的新手,在学习Python时,我编写了一个简单的过程程序,它接受URL并将数据从URL提取到文本文件

代码看起来像这样, 忽略变量,这是虚构的

def open_URL(url):
    # try the URL
    # except errors

def fetch_raw_content(url):
    # fetch the raw content from web
    # return raw_content

def clean_up_raw(raw_content):
    # clean up html tags and stuff
    # format the raw content
    # return content

def write_to_file(filename, content):
    # create a file with filename
    # open the file
    # write the content
    # close the file


raw = fetch_raw_content(open(open_URL("somesite.com")))

content = clean_up_raw(raw)
write_to_file(content)

我想知道这是否是一种面向对象的方法,因为我对面向对象世界的接触是有限的,如果有人建议我用某种方法来面向对象这个程序,谢谢

OOP是一种用来解决代码设计问题的工具。您的设计没有问题,也没有什么需要修复的。如果您确实需要代码更加抽象和可配置,OOP将只为自己付费。例如,在以后的某个时候,您可能会意识到您的内容可以从不同的来源获得,而不仅仅是URL-文件、数据库等。在这种情况下,您可以通过创建抽象类读取器来对阅读部分进行OOP,并为每种类型的源创建特定的读取器:

 class Reader:
     def read():
        # return the content

 class URLReader(Reader)...

 class FileReader(Reader)...

 class DatabaseReader(Reader)...
类似地,如果清理过程可以分为多个阶段,并且只希望在特定情况下应用其中的一部分,则可以如下所示对其进行抽象:

 class Action:
    def apply(content):
        # apply the action to the content

 class RemoveWhiteSpace(Action)...

 class RemoveFontTags(Action)...

 class OptimizeLineBreaks(Action)...

 class Sequence(Action)...
然后:

 content = Sequence(RemoveWhiteSpace(), OptimizeLineBreaks()).apply(content)

再说一次,如果你真的需要那么多的灵活性,这些努力只会为自己带来回报。在大多数情况下,您可以解决Python中的任何问题,而无需使用class关键字。

这里不需要OOP,只需要一个入口点函数,它接受url和文件名,然后依次调用其他函数。FWIW,我想您这样做是为了练习,但是,如果您真的需要用Python进行一些严肃的web抓取,您可能需要查看一下。