How To: Get the rendered HTML of a webpage with Python

Jun 7, 2013

Hello again!

This time I am going to show you how to get the rendered HTML of a webpage using Python. For this task we need only ten lines of code and to import urllib2, the library that it will help us. The rendered HTML of a webpage is the HTML that our web-browser receives from the server and renders on the screen. In simple words, this is what we see on the screen and not the code used to generate the webpage.

This HTML code I use to find specific tags into the code.

Here is the code:

def get_page_code(link):
    import urllib2                 # import the necessary library
    html = ""                      # initiate an empty string as the variable that holds the html code
    req = urllib2.Request(link)    # initiate a request
    try:
        response = urllib2.urlopen(req) # store the response in a file
        html = response.read()     # read the content of the file
        response.close()           # close the file
    except ValueError:
        return html                # if there is an error return the empty string
    return html                    # return the generated html

Keep in mind that you will get a long string as output.

Tags: programmingpython

Archives

  1. February 2024
  2. Maximizing Software Development Productivity: The Power of Flow and Minimizing Interruptions
  3. December 2023
  4. Clean Code in Java: Writing Code that Speaks
  5. Clean Code in Java: A concise guide
  6. Understanding Value Objects in Java: A Brief Guide
  7. August 2023
  8. Consuming RabbitMQ Messages with Clojure: A Step-by-Step Tutorial with Tests
  9. January 2023
  10. Running a Spring Boot service with kubernetes
  11. December 2022
  12. Hosting a PWA with Jekyll and Github pages
  13. November 2022
  14. Global Day of Code Retreat
  15. Facilitating a mini Code Retreat
  16. October 2022
  17. The Curse of Optional
  18. September 2022
  19. Testing Spring Boot Microservices - Presentation
  20. March 2022
  21. TDD Workshop
  22. February 2022
  23. Value Objects in Java
  24. Efficient Java
  25. January 2022
  26. Spring Boot testing - Focus on your changes
  27. Product users - Personas
  28. December 2021
  29. Write code fit for testing
  30. November 2020
  31. Running a Spring Boot app with kubernetes
  32. September 2019
  33. Setup GPG on Mac and sign git repositories
  34. July 2019
  35. Running a Clojure Pedestal application on Raspberry Pi model B revision 2
  36. Clojure from zero to hero (part 3) - First endpoint
  37. Clojure from zero to hero (part 2) - A bit of syntax
  38. June 2019
  39. Clojure from zero to hero (1) - explaining project.clj
  40. Clojure from zero to hero (0) - creating a Pedestal app
  41. November 2017
  42. Introduction to Docker
  43. April 2015
  44. Git micro commits
  45. July 2014
  46. Google Glass Development - setup tools, environment and turn on debugging on Glass
  47. June 2013
  48. How To: Get the rendered HTML of a webpage with Python
  49. Set union of two lists in Python