-1

I want to write a method to parse a site with requests library, the method should take a part of url having base_url in it and perform the get request on this, the main problem is that I do not know how to make it better;

What I have in mind now is:

import requests

class Response:

# ...

def site_parser(self, atom):

    base_url="https://example.com/"

    def category1(self):
        return requests.get(base_url + category1/ + atom).text

    def category2(self):
        return requests.get(base_url + category2/ + atom).text

if __name == "__main__":

def main():

    result = Response()
    result.site_parser.category1("atom")
    result.site_parser.category2("atom")

so needed data has the same base url but different dirs to get into, and I need to gen each dir if only the method was called afterwards. is there a way of doing this properly? I wouuld like to avoid making base url global variable

Mark Wrings
  • 21
  • 2
  • 5
  • You can define nested functions like that, but you can't call them like that. – tobias_k Nov 30 '18 at 14:20
  • It is possible to define `category1` as `site_parser.category1` under `site_parser`, but I don't see the advantage in doing this. In that case `site_parser` really should be a `class`, no? – r.ook Nov 30 '18 at 14:21
  • @tobias_k, but is this possible to call them at all, if they were declared this way I wrote? – Mark Wrings Nov 30 '18 at 16:51

1 Answers1

2

It seems to me that what you need is another class.

class Response:

    # ... Some dark magic here ...

    def site_parser(self, atom):
        return ResponseParser(self, atom)

class ResponseParser:

    def __init__(self, res, atom):
        self.atom = atom
        self.res = res
        self.base_url = "https://example.com/"

    def category1(self):
        # ... Do stuff ...

    def category2(self):
        # ... Do stuff ...

Then you call it with

result = Response()
result.site_parser("atom").category1()

If you really insist on getting rid of the parentheses on the site_parser call, you could move the "atom" bit to the categoryN methods and turn site_parser into a property, but IMO that would probably just confuse people more than anything.

As a functional programmer, I love nested functions and closures as much as the next guy, but it seems to me that, based on the limited example you've given, having a second helper class is probably going to be the more readable way to go about this in this case.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116