Skip to content

Instantly share code, notes, and snippets.

@reaganscofield
Created August 23, 2019 05:06
Show Gist options
  • Select an option

  • Save reaganscofield/fe8249881d980b77dbeeff2e441be68c to your computer and use it in GitHub Desktop.

Select an option

Save reaganscofield/fe8249881d980b77dbeeff2e441be68c to your computer and use it in GitHub Desktop.
import requests
from requests import get
import urllib
from bs4 import BeautifulSoup
from urllib.error import HTTPError
from urllib.request import urlopen
login_url = 'https://slashdot.org/my/login'
scraping_url = 'https://slashdot.org'
class SlashDot:
def actions():
username = input("Enter username : ")
password = input("Enter password : ")
if username and password:
session = requests.Session()
per_session = session.post(login_url, data={'unickname': username, 'upasswd': password})
if per_session:
try:
BeautifulSoup(session.get(login_url).content, 'html.parser')
response = get(scraping_url)
html_soup = BeautifulSoup(response.text, 'html.parser')
articles = html_soup.find_all('article', class_ = 'article')
articles_data = []
for article in articles[:3]:
data = {
"headline": article.header.h2.find('span', class_ = 'story-title').a.text,
"author": article.header.find('div', class_ = 'details').find('span', class_ = "story-byline").find('a'),
"date": article.header.find('div', class_ = 'details').find('span', class_ = "story-byline").time.text
}
articles_data.append(data)
if len(articles_data) > 0:
for article in articles_data:
print(article)
else:
print("articles not found!")
except HTTPError as error:
print(error)
else:
print("username and password is required")
SlashDot.actions()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment