首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >检测网页编码+读取网页内容 原

检测网页编码+读取网页内容 原

作者头像
双面人
发布2019-04-10 16:49:52
发布2019-04-10 16:49:52
2.5K0
举报
文章被收录于专栏:热爱IT热爱IT

import chardet #字符集检测 import urllib.request # 网址 url = "http://www.baidu.com/"

def automatic_detect(url):      content=urllib.request.urlopen(url).read()      result=chardet.detect(content)     encoding=result['encoding']      return encoding 

print(url)  print(automatic_detect(url))

# 请求 request = urllib.request.Request(url) # 爬取结果 response = urllib.request.urlopen(request) data = response.read() # 设置解码方式 data = data.decode('utf-8')

# 打印爬取网页的各类信息 # 打印结果 print(data) print(type(response)) print(response.geturl()) print(response.info()) print(response.getcode())

需要安装chardet

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/05/30 ,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档