2 回答
TA贡献1804条经验 获得超2个赞
你可以这样做(你还需要正确使用请求和 url 作为字符串):
class Request_class(object):
'''
Class with all the requests
'''
def __init__(self, linkedin_url):
'''
Constructor
'''
#You can pass variables to create some class properties
#for example, if linkedin url is always the same
self.linkedin = linkedin_url
def facebook(self, url):
facebook = request_url(url)
facebook.apply(...)
...
def instagram(self, url):
instagram = request_url(url)
instagram.apply(...)
...
def linkedin(self):
#you don't need to pass the linkedin url; it's already a class property
linkedin = request_url(self.url)
linkedin.apply(...)
...
#Then you can create an instance of the class:
my_requests = Request_class('www.linkedin.com')
#And you can call its methods:
my_requests.facebook('www.facebook.com')
my_requests.instagram('www.instagram.com')
#For this last the class already knows the url
my_requests.linkedin()
TA贡献1934条经验 获得超2个赞
首先创建 RequestClass 并给它一个 url 的属性。然后您可以为每个站点使用不同的 url 创建对象。some_function() 可以是类方法:
class RequestClass:
def __init__(self, url):
self.url = url
def make_request(self):
request(self.url)
def some_function(self):
apply(...)
要创建一个对象:
fb = RequestClass("www.facebook.com")
ig = RequestClass("www.instagram.com")
li = RequestClass("www.linkedin.com")
fb.make_request()
ig.make_request()
li.make_request()
如果你想访问请求中的数据,你可以在你的请求方法中将它保存为 self.data 假设你向站点发出请求的方式返回数据:
class RequestClass:
def __init__(self, url):
self.url = url
self.data
def make_request(self):
self.data = request(self.url)
要访问数据,例如,来自 Facebook 的请求:
fb.data #returns the data from the request to facebook
process_data(fb.data) #easily use that data in a diff function
如果您在类中创建其他方法或属性,则可以轻松地为每个站点访问它们,例如,如果您想要打印请求中的数据:
fb.print_request_data()
ig.print_request_data()
li.print_request_data()
添加回答
举报