Read another website with Python 3.10.6
## Library versions
## Django==4.1
## beautifulsoup4==4.11.1
## python==3.10.6
from urllib.request import urlopen
from bs4 import BeautifulSoup
# Below code is reading the page title and description if can be found on the web page.
myTitle=''
myDescription=''
# Prime the page of the website to be read.
myURL='{{insert webpage url here e.g. https://www.example.com}}'
# Setup a request simulating a real user ready to access an external site.
myRequest = urllib.request.Request(url=myURL, headers={'User-Agent':' Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0'})
# Read the page.
external_site_html = urlopen( myRequest ).read()
#
# If the page was found setup a parser to read various elements of interest from the page.
if external_site_html:
soup = BeautifulSoup(external_site_html, "html.parser")
# Read the title and description if present on the webpage
if soup.title.string:
myTitle = soup.title.string
desc = soup.find(attrs={'name':'description'})
if desc:
if desc['content']:
myDescription=desc['content']