【发布时间】:2025-12-26 20:30:06
【问题描述】:
直截了当,我是面向对象编程的新手,在学习 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,只需一个“入口点”函数,它接受
url和filename并依次调用您的其他函数。 -
FWIW,我想你这样做是为了练习,但如果你真的需要在 Python 中进行一些严肃的网络抓取,你可能需要查看Scrapy。
标签: python oop design-patterns object-oriented-analysis