Skip to main content

Read The Hacker News in efficient way using Python Programming.


import requests
from bs4 import BeautifulSoup 
import pprint
url='https://news.ycombinator.com/'
url2='https://news.ycombinator.com/news?p=2'
res=requests.get(url)
res2=requests.get(url2)

soup=BeautifulSoup(res.text,'html.parser')
soup2=BeautifulSoup(res2.text,'html.parser')
links=soup.select('.storylink')
subtext=soup.select('.subtext')
links2=soup2.select('.storylink')
subtext2=soup2.select('.subtext')

def sorting_votes(hnlist):
  return sorted(hnlist,key=lambda k:k['votes'],reverse=True)
megalinks=links + links2
megasubtext=subtext+subtext2

def custom_hn(links,subtext):
  hn=[]
  for idx,item in enumerate(links):
    title=links[idx].getText()
    href=links[idx].get('href',None)
    vote=subtext[idx].select('.score')
    
    if len(vote):
      points=int(vote[0].getText().replace(' points',''))
      if points >100:
        hn.append({'tittle':title, 'link': href, 'votes':points})
  return sorting_votes(hn)

pprint.pprint(custom_hn(megalinks,megasubtext))




#Advantages You don't need to read all the unnecessary links, you can filter them out
and read-only popular one with chronological order.

Comments

Popular posts from this blog

Use of formula loop plot of graph y=sin(x)/x and y=sin(x^2)/x in LabVIEW

1. Formula loop Using formula loop compute y for the following two functions: 𝑦1 = sin(x)/x and 𝑦2 = sin(x2)/x where x varies from 0 to 10 in step of 0.05. Plot 𝑦1 & 𝑦2 vs. x in a single XY graph. Use For Loop and Formula loop for this problem.

Separate negative and positive value from the array using structure function in LabVIEW

Great Architecture clips

Picture credit by Simran (my little sister) Lots of thanks 😊