[Solved] Can't understand some dot notations used in a code written in PYTHON



  • Hi Ranjith! Hope you are doing well. Recently, going through a Python code I got stuck at some point and can't understand THE DOT OPERATOR used to call the LINKS from THE APP CLASS by THE TWO LINES projected outward with equal to symbol ending up with THIS ONE WITHIN CRAWLER METHOD. The three lines which made me confuse are marked with this one. My question is how come PPN and CURRENT_APP call the LINKS with dot notation? Thanks in advance.

    from lxml import html
    import requests
    
    class app_crawler:
    	def __init__(self,starturl,depth):
    		self.starturl=starturl
    		self.depth=depth
    		self.current_depth=0
    		self.depth_links=[]
    		self.apps=[]
    	def crawler(self):
    		ppn=self.get_app(self.starturl)
    		self.apps.append(ppn)
    		self.depth_links.append(ppn.links)======================[This One]
    		while self.current_depth < self.depth:
    			current_links=[]
    			for link in self.depth_links[self.current_depth]:
    				current_app=self.get_app(link)
    				current_links.extend(current_app.links)===============[This One]
    				self.apps.append(current_app)
    				self.current_depth+=1
    				self.depth_links.append(current_links)
    
    
    	def get_app(self,link):
    		page = requests.get(link)
    		tree = html.fromstring(page.text)
    		name = tree.xpath('//h1[@itemprop="name"]/text()')[0]
    		developper=tree.xpath('//div[@class="left"]/h2/text()')[0]
    		price=tree.xpath('//div[@itemprop="price"]/text()')[0]
    		links=tree.xpath('//div[@class="lockup-info"]//*/a[@class="name"][email protected]')
    		app=App(name, developper, price, links)
    		return app
    
    
    class App:
    	def __init__(self,name,developper,price,links):
    		self.name=name
    		self.developper=developper
    		self.price=price
    		self.links=links=============================[This One]
    
    	def __str__(self):
    		return ("Name: "+self.name +"\nDevelopper: "+self.developper+"\nPrice: "+self.price+"\n")
    
    crawl=app_crawler("https://itunes.apple.com/us/app/candy-crush-saga/id553834731?mt=8",1)
    crawl.crawler()
    for data in crawl.apps:
    	print(data)

  • administrators

    @shahin2137 links property is set to links extracted from the page in App class definition. So links property can always be retrieved using .links.

    ppn is an instance of class App so links can be called on ppn



  • @ranjithkumar10
    Thanks for your answer. Now it all make sense to me.


guest-login-reply
 

reconnecting-message