1
\$\begingroup\$

I'm using my crawler class in the following manner and I'm beginning to think it's bad practice:

crawler.py

import requests
class Crawler():
 def __init__(self, url):
 self.url = url
 def web_crawler(self):
 requests.get(self.url)
 return requests.text

main.py

for url in urls:
 crawler = Crawler(url)
 results = crawler.web_crawler()

Would it be better to move the url parameter outside of Crawler's __init__ and move it into the web_crawler function? That way the class won't have to be reinitialized multiple times in main.py.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 6, 2016 at 13:37
\$\endgroup\$
1
  • 1
    \$\begingroup\$ return requests.text? Did you, at least, tried to run this code? \$\endgroup\$ Commented Feb 6, 2016 at 14:09

2 Answers 2

5
\$\begingroup\$

As the Crawler class just has one method along with __init__, you can avoid a class altogether and write:

def web_crawler(url):
 requests.get(url)
 return requests.text

You now have to initialize exactly 0 times, thus removing the problem from the root:

for url in urls:
 results = web_crawler(url)

The code is also simplified, both in definition and usage.

301_Moved_Permanently
29.4k3 gold badges49 silver badges98 bronze badges
answered Feb 6, 2016 at 14:10
\$\endgroup\$
1
\$\begingroup\$

You can also create a field name url, and use getter and setter to obtain/change the value outside of the class.

answered Feb 6, 2016 at 14:02
\$\endgroup\$
3
  • \$\begingroup\$ This may be the start of a good review, but in it's current form it doesn't provide much value. Would you care to expand a bit? \$\endgroup\$ Commented Feb 6, 2016 at 14:08
  • 3
    \$\begingroup\$ Would've been a good idea but I don't think it's a good practice. This reminds me of Java. \$\endgroup\$ Commented Feb 6, 2016 at 14:46
  • \$\begingroup\$ Well I just noted that you can take the variable out and provide mechanism for changing it, this way you will need to change a variable only. Also I must note that you may have problems with multi threading if it isn't implemented properly. \$\endgroup\$ Commented Feb 6, 2016 at 15:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.