Python Course Zero to Mastery from Udemy Blog Post Part-2

Part 1: Link

Twitter Bot for various Activities:

import time

import tweepy

auth=tweepy.OAuthHandler('','' )
auth.set_access_token('','')

api=tweepy.API(auth)

# public_tweets=api.home_timeline()
# for tweettts in public_tweets:
#     print(tweettts.text)
user=api.me()
# print(user.name)
# print(user.screen_name)
# print(user.followers_count)

def limit_handler(cursor):
    try:
        while True:
            yield cursor.next()
    except tweepy.RateLimitError:
        time.sleep(300)

search_string='Germany'
numbersOfTweets=2

for tweet in tweepy.Cursor(api.search,search_string).items(numbersOfTweets):
    try:
        #tweet.favorite()
        tweet.retweet()
        print("I liked that tweet")
    except tweepy.TweepError as e:
        print(e.reason)
    except StopIteration:
        break
#generous bots
# for  follower in limit_handler(tweepy.Cursor(api.followers).items()):
#     if follower.name=='Edouraid':
#        follower.follow()
#       #print(follower.name)

Web Scraping Code:

import requests
from bs4 import BeautifulSoup
import pprint

res= requests.get('https://news.ycombinator.com/news')
res2=requests.get('https://news.ycombinator.com/news?p=2')
#print(res.text)
soup=BeautifulSoup(res.text,'html.parser')
soup2=BeautifulSoup(res.text,'html.parser')
#print(soup.body.contents)
#print(soup.find_all('div'))
links=soup.select('.title')
subtext=soup.select('.subtext')

links2=soup2.select('.title')
subtext2=soup2.select('.subtext')

mega_links=links+links2
mega_subtext=subtext+subtext2

def sort_stories_by_votes(hnlist):
    return sorted(hnlist, key=lambda k:k['votes'])

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

pprint.pprint(create_custom_hn(mega_links,mega_subtext))

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *