How to handle response encoding from urllib.request.urlopen()

后端 未结 7 2134
一向
一向 2020-11-27 03:52

I\'m trying to search a webpage using regular expressions, but I\'m getting the following error:

TypeError: can\'t use a string pattern on a bytes-lik

相关标签:
7条回答
  • 2020-11-27 04:52

    Here is an example simple http request (that I tested and works)...

    address = "http://stackoverflow.com"    
    urllib.request.urlopen(address).read().decode('utf-8')
    

    Make sure to read the documentation.

    https://docs.python.org/3/library/urllib.request.html

    If you want to do something more detailed GET/POST REQUEST.

    import urllib.request
    # HTTP REQUEST of some address
    def REQUEST(address):
        req = urllib.request.Request(address)
        req.add_header('User-Agent', 'NAME (Linux/MacOS; FROM, USA)')
        response = urllib.request.urlopen(req)
        html = response.read().decode('utf-8')  # make sure its all text not binary
        print("REQUEST (ONLINE): " + address)
        return html
    
    0 讨论(0)
提交回复
热议问题