Search code examples
pythonurllib2

Urllib2 get specific element


I have a web-page and I want to get the <div class="password"> element using urllbi2 in Python without using Beautiful Soup.

My code so far:

import urllib.request as urllib2
link = "http://www.chiquitooenterprise.com/password"
response = urllib2.urlopen('http://www.chiquitooenterprise.com/')
contents = response.read('password')

It gives an error.


Solution

  • You need to decode() the response with utf-8 as it states in the Network tab:

    out

    Hence:

    import urllib.request as urllib2
    link = "http://www.chiquitooenterprise.com/password"
    response = urllib2.urlopen('http://www.chiquitooenterprise.com/')
    output = response.read().decode('utf-8')
    print(output)
    

    OUTPUT:

    YOIYEDGXPU