Remove newline in python with urllib

前端 未结 3 486
名媛妹妹
名媛妹妹 2021-01-16 06:27

I am using Python 3.x. While using urllib.request to download the webpage, i am getting a lot of \\n in between. I am trying to remove it using the

3条回答
  •  花落未央
    2021-01-16 07:00

    If you look at the source you've downloaded, the \n escape sequences you're trying to replace() are actually escaped themselves: \\n. Try this instead:

    import urllib.request
    
    def download_page(a):
        opener = urllib.request.FancyURLopener({})
        open_url = opener.open(a)
        page = str(open_url.read()).replace('\\n', '')
        return page
    

    I removed the try/except clause because generic except statements without targeting a specific exception (or class of exceptions) are generally bad. If it fails, you have no idea why.

提交回复
热议问题