Skip to content

Instantly share code, notes, and snippets.

@meesont
Last active May 17, 2019 14:14
Show Gist options
  • Save meesont/96912eebd05d95a94b6208ceb7e8efe5 to your computer and use it in GitHub Desktop.
Save meesont/96912eebd05d95a94b6208ceb7e8efe5 to your computer and use it in GitHub Desktop.
Python3 Reference Generator for StackOverflow
# @Author: Thomas Meeson <thomas>
# @Date: 18-02-2019
# @Last modified by: thomas
# @Last modified time: 25-03-2019
# @License: Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# @Copyright: Copyright 2018 Thomas Meeson
import requests as r
from bs4 import BeautifulSoup as bs
import datetime as dt
#This is specific to stackoverflow as it finds specific classes which are used on stackoverflow to define titles ect
def createRef(link):
response = r.get(link)
soup = bs(response.content, 'html.parser')
title = soup.find('div', id='question-header').find('a', class_='question-hyperlink').get_text() #This gets the title
creationTime = soup.find('div', class_='user-action-time').find('span').get('title').split(' ')
creationTime = creationTime[0].split('-')
creationDate = creationTime[0]
now = dt.datetime.now()
outString = f"Stackoverflow ({creationDate}) '{title}' [online] {link} accessed {now.day}/{now.month}/{now.year}"
return outString
if __name__ == '__main__':
link = 'https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date'
print(createRef(link))
@meesont
Copy link
Author

meesont commented May 17, 2019

To run, change the link to whichever question you would like, for example:
link = 'https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date'

and run

python3 refGen.py

whilst in the same directory as the file.

Note, this requires BeautifulSoup 4 and Requests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment