I've written a very tiny script using class to scrape some titles of products from craigslist. My intention is to make use of __str__() method so that my script can print the result without explicitly calling that very method.

However, when I execute my script in the following way, I get the desired results.

Is this the only way I can print the result or there is anything better to print the result through dunder str method?

import requests
from bs4 import BeautifulSoup

URL = "http://bangalore.craigslist.co.in/search/rea?s=120"

class DataSourcer:

    def __init__(self,link):
        self.link = link
        self.datalist = []

    def fetch(self):
        res = requests.get(self.link)
        soup = BeautifulSoup(res.text,"lxml")
        for ilink in soup.select("li.result-row a[data-id]"):

        # return self.datalist #I do not wish to return "self.datalist" here

    def __str__(self):
        return self.datalist

if __name__ == '__main__':    
    crawler = DataSourcer(URL)
    print(crawler.__str__()) #I found it working this way
  • \$\begingroup\$ I'm not entirely sure what you're asking, but: print(crawler.__str__()) should be print(str(crawler)). \$\endgroup\$ – Dair Dec 8 '18 at 21:50
  • \$\begingroup\$ That is exactly what you have surmised already I'm asking. Your suggested approach threw this error TypeError: __str__ returned non-string (type list) @Dair . Thanks. \$\endgroup\$ – MITHU Dec 8 '18 at 21:54
  • 3
    \$\begingroup\$ You should instead do return str(self.datalist). def __str__ should return a string type. Instead, you have returned a list. You can convert the list to a string by calling str, so return str(self.datalist). \$\endgroup\$ – Dair Dec 8 '18 at 21:57
  • \$\begingroup\$ Yep, this leads me to a better approach @Dair. Is there any way I can print the result in regular strings other than the list. I know I'm wrong but I tried it for item in crawler: print(item) and failed miserably. However, I get the result when i try like this (in my existing script) for item in crawler.__str__(): print(item). Thanks. \$\endgroup\$ – MITHU Dec 8 '18 at 22:12
  • 1
    \$\begingroup\$ return "\n".join(self.datalist) would be the right way instead of for ...: print(...). \$\endgroup\$ – Graipher Dec 9 '18 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.