Page 182 - Programming With Python 3
P. 182

An Introduction to STEM Programming with Python — 2019-09-03a                              Page 169
            Chapter 15 — Reading Data from the Web

             29|  """
             30|
             Free
             31|  soup = bs4.BeautifulSoup(page, 'html.parser')
             32|
             33|  # get the linked pages from te anchors on page
             34|  tags = soup.select('a')
             35|  for tag in tags:
             36|     print(tag['href'])
             eBook
             37|
             38|  # get the attribute language from the html tag
             39|  # it may not exist - if not use unknown
             40|  tag = soup.select("html")[0]
             41|  print("this page's language is", tag.get('lang','unknown'))
             42|
             Edition
             43|  # create a comma separated printout of the
             44|  # string content of all th and td tags
             45|  rows = soup.select("table tr")
             46|  for row in rows:
             47|      cols = row.select("th,td")
             48|      print(cols[0].string, ',', cols[1].string)
            Please support this work at
             49|
             50|  # show text on first row - strip out all html
             51|  print(rows[0].text)
                                  http://syw2l.org
                    http://www.syw2l.org
                    https://www.crummy.com/software/BeautifulSoup/
                    http://www.python.org
                    this page's language is en
                    Fruit , Color                                              Free
                    Banana , Yellow
                    Lemon , Yellow
                    Orange , Orange
                    FruitColor
                                                                   eBook
            Another example that gets the current weather observation at an airport from the U.S. National Weather
            Service, follows:

               1|  import bs4
               2|  import urllib.request
                                                                Edition
               3|
               4|  #change city code to four letter US airport code
               5|  # KLAX-Los Angeles, KJFK-New York City, KDWU - Ashland Kentucky
               6|  city = "KDWU"



            Copyright 2019 — James M. Reneau Ph.D. — http://www.syw2l.org — This work is licensed
            under a Creative Commons Attribution-ShareAlike 4.0 International License.
   177   178   179   180   181   182   183   184   185   186   187