|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
大家好,我下面是做代理IP实践,在网上找也找不到一些解决方法。结果不正常。
import urllib.request
url = 'https://www.ip138.com'
proxy_suport = urllib.request.ProxyHandler({'http':'113.77.101.202:8118'})
opener = urllib.request.build_opener(proxy_suport)
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'}
#urllib.request.install_opener(opener)
request = urllib.request.Request(url,headers = header)
response = opener.open(request)
html = response.read().decode('utf-8')
html
结果输出:
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-4-25b1b7606623> in <module>
7 request = urllib.request.Request(url,headers = header)
8 response = opener.open(request)
----> 9 html = response.read().decode('utf-8')
10 html
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb2 in position 177: invalid start byte
把网址换成baidu.com就出现一堆东西,不正常。
'<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta content="always" name="referrer"><meta name="theme-color" content="#2932e1"><link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" /><link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" title="百度搜索" /><link rel="icon" sizes="any" mask href="//www.baidu.com/img/baidu_85beaf5496f291521eb75ba38eacbd87.svg"><link rel="dns-prefetch" href="//dss0.bdstatic.com"/><link rel="dns-prefetch" href="//dss1.bdstatic.com"/><link rel="dns-prefetch" href="//ss1.bdstatic.com"/><link rel="dns-prefetch" href="//sp0.baidu.com"/><link rel="dns-prefetch" href="//sp1.baidu.com"/><link rel="dns-prefetch" href="//sp2.baidu.com"/><title>百度一下,你就知道</t
1.可能网址没有使用utf-8作为解码,一部分网站可能使用GBK或者ANSI,可以使用try except解决
2.你这不是成功了吗,这个就是百度主页的html代码呀
|
|