Scraping and printing titles from Craigslist

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:

self.datalist = []

def fetch(self):
soup = BeautifulSoup(res.text,"lxml")

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

def __str__(self):
return self.datalist

if __name__ == '__main__':
crawler = DataSourcer(URL)
crawler.fetch()
print(crawler.__str__()) #I found it working this way

• I'm not entirely sure what you're asking, but: print(crawler.__str__()) should be print(str(crawler)). – Dair Dec 8 '18 at 21:50
• 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. – MITHU Dec 8 '18 at 21:54
• 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). – Dair Dec 8 '18 at 21:57
• 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. – MITHU Dec 8 '18 at 22:12
• return "\n".join(self.datalist) would be the right way instead of for ...: print(...). – Graipher Dec 9 '18 at 9:51