0

The code failed.

Could anyone help?

I search in stack overflow, and tried some methods, but I can't seem to find any that work.

from urllib.request import urlopen

link = "https://www.youtube.com/live_chat?v=IKQkjWmqQv8&is_popout=1"

f = urlopen(link)
myfile = f.read()
print (myfile)
output_file = open('1.txt','w')
output_file.write(myfile)
output_file.close()
khelwood
  • 52,115
  • 13
  • 74
  • 94
fizz
  • 59
  • 6

3 Answers3

2

Try this code:

from urllib.request import urlopen

link = "https://www.youtube.com/live_chat?v=IKQkjWmqQv8&is_popout=1"

f = urlopen(link)
myfile = f.read()
print (myfile)
output_file = open('1.txt','wb')
output_file.write(myfile)
output_file.close()
Eivind
  • 44
  • 5
  • Thank you. It works. However the content of the website cannot be loaded. It said the browser version does not support – fizz Sep 28 '17 at 06:16
1

You are getting a bytes back from your read(), you could just write this to your file in binary mode by adding a b as follows:

from urllib.request import urlopen

link = "https://www.youtube.com/live_chat?v=IKQkjWmqQv8&is_popout=1"

f = urlopen(link)
myfile = f.read()
print (myfile)

with open('output.html','wb') as output_file:
    output_file.write(myfile)

Note: Using with is the preferred method for dealing with files. It will automatically close your file for you afterwards.

Martin Evans
  • 43,220
  • 16
  • 78
  • 90
  • Thank you. It works. However the content of the website cannot be loaded. It said the browser version does not support. – fizz Sep 28 '17 at 06:16
  • I suggest you rename the output file to `output.html` and try loading that into you browser. – Martin Evans Sep 28 '17 at 09:07
0

Other answers are true, but you can decode bytes to string. To do that, just modify these 2 lines:

...
output_file = open('1.txt', 'w', encoding="utf8")
output_file.write(myfile.decode("utf8"))
...
Alperen
  • 3,004
  • 2
  • 20
  • 42
  • Thank you. It works. However the content of the website cannot be loaded. It said the browser version does not support – fizz Sep 28 '17 at 09:01