Note 13.12.1.
The XML format is described in the next chapter.
pip
is available at:urllib
to read the page and then use BeautifulSoup
to extract the href
attributes from the anchor (a
) tags.import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = "https://docs.python.org"
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')
# Retrieve all of the anchor tags
tags = soup('a')
for tag in tags:
print(tag.get('href', None))
href
attribute for each tag.from urllib.request import urlopen
from bs4 import BeautifulSoup
import ssl
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = "http://www.dr-chuck.com/page1.htm"
html = urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, "html.parser")
# Retrieve all of the anchor tags
tags = soup('a')
for tag in tags:
# Look at the parts of a tag
print('TAG:', tag)
print('URL:', tag.get('href', None))
print('Contents:', tag.contents[0])
print('Attrs:', tag.attrs)
html.parser
is the HTML parser included in the standard Python 3 library. Information on other HTML parsers is available at:https://pypi.python.org/pypi/beautifulsoup4
https://packaging.python.org/tutorials/installing-packages/
http://
https://
http://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser