37,719
社区成员
发帖
与我相关
我的任务
分享
rep=urllib.request.Request(url)
response=urllib.request.urlopen(rep)
page=response.read()#获得网页
way=self.way+str(self.num)+r'.html'
self.num+=1
file=open(way,'w')
m_charset = re.search('<meta\s*http-equiv="?Content-Type"? content="text/html;\s*charset=([\w\d-]+?)"', page.decode("ISO-8859-1"), re.IGNORECASE)
if m_charset.group(1) :
page=page.decode(m_charset.group(1),'ignore')
file.write(str(page))
file.close()
if m_charset.group(1) and (m_charset.group(1)!=r'utf-8'):
page=page.decode(m_charset.group(1),'ignore')
if m_charset.group(1) :
file=open(way,'w', encoding=m_charset.group(1))