The following code works well.
from urllib.request import urlopen
import bs4
doc= urlopen("http://www.w3school.com.cn/html/html_tables.asp")
soup = bs4.BeautifulSoup(doc,fromEncoding="GB2312")
a=soup.findAll("title")
print (soup.prettify())
output = open("C:\\Users\\yfeng14\\Desktop\\betting\\contents.txt", 'w', encoding="UTF-8")
output.write(soup.prettify())
output.close()
If we use "requests" package, it fails.
import requests
import bs4
output = open("C:\\Users\\yfeng14\\Desktop\\betting\\contents.txt", 'w', encoding="UTF-8")
request_link = "http://www.songtaste.com/"
response = requests.get(request_link)<span style="white-space:pre"> </span>
soup = bs4.BeautifulSoup(response.text,"html.parser",from_encoding="GB2312")
output.write(soup.prettify())
output.close()