Skip to content

Instantly share code, notes, and snippets.

@clamytoe
Created May 12, 2020 09:39
Show Gist options
  • Save clamytoe/eef03793c8a05d4853b247ddb32664c1 to your computer and use it in GitHub Desktop.
Save clamytoe/eef03793c8a05d4853b247ddb32664c1 to your computer and use it in GitHub Desktop.
Small little script to retrieve the keywords from any web page.
#!/usr/bin/env python3
"""keywords.py
Small little script to retrieve the keywords from any web page.
You can either pass the url for the page as an argument or enter
it when prompted for it.
Example:
keywords.py https://www.youtube.com/watch?v=YrHlHbtiSM0
The keywords get printed to the console and are also copied to
your system's clipboard and are ready to paste.
"""
from sys import argv
from typing import Optional
import pyperclip # type: ignore
import requests
from bs4 import BeautifulSoup as Soup # type: ignore
def get_soup(url: str) -> Optional[Soup]:
response = requests.get(url)
if response.ok:
return Soup(response.content, "html.parser")
return None
def main() -> None:
if len(argv) > 1:
url = argv[1]
else:
url = input("enter url: ")
soup = get_soup(url)
if soup is None:
print("Timed out...")
exit()
keywords = soup.find("meta", {"name": "keywords"})["content"]
print(keywords)
pyperclip.copy(keywords)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment