import re #正则表达式模块
import urllib.request
import time #时间模块
import string #字符串模块
def getHtml(url):f=urllib.request.urlopen(url)
print(f.read())
if name == '__main__':
getHtml('/')
下面的是获取网页代码不乱码
import re #正则表达式模块
import urllib.request
import time #时间模块
import string #字符串模块
def getHtml(url):f=urllib.request.urlopen(url)
print(str(f.read(),'utf-8'))
if name == '__main__':getHtml('/')
第三种获取网页源代码方法
import requests
req = requests.get("/")
if req.encoding == 'ISO-8859-1':encodings = requests.utils.get_encodings_from_content(req.text)
if encodings:
encoding = encodings[0]
else:
encoding = req.apparent_encoding
# encode_content = req.content.decode(encoding, 'replace').encode('utf-8', 'replace')
global encode_content
encode_content = req.content.decode(encoding, 'replace') #如果设置为replace,则会用?取代非法字符;
print(encode_content)
with open('test.html','w',encoding='utf-8') as f:f.write(encode_content)
赏
支付宝打赏
微信打赏
如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!