HTTP Error 403没有了,但是中文全都是乱码。又是怎么回事?
首先是简单的网页抓取程序:
[python] import sys, urllib2
req = urllib2.Request("http://blog.csdn.net/nevasun")
fd = urllib2.urlopen(req)
while True:
data = fd.read(1024)
if not len(data):
break
sys.stdout.write(data)
import sys, urllib2
req = urllib2.Request("http://blog.csdn.net/nevasun")
fd = urllib2.urlopen(req)
while True:
data = fd.read(1024)
if not len(data):
break
sys.stdout.write(data)
在终端运行提示urllib2.HTTPError: HTTP Error 403: Forbidden,怎么回事呢?
这是由于网站禁止爬虫,可以在请求加上头信息,伪装成浏览器访问。添加和修改:
[python] headers = {'User-Agent':'Mozilla/5.0 (windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
req = urllib2.Request("http://blog.csdn.net/nevasun", headers=headers)
headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
req = urllib2.Request("http://blog.csdn.net/nevasun", headers=headers)
再试一下,2881064151HTTP Error 403没有了,但是中文全都是乱码。又是怎么回事?
这是由于网站是utf-8编码的,需要转换成本地系统的编码格式:
[python]import sys, urllib2
headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
req = urllib2.Request("http://blog.csdn.net/nevasun", headers=headers)
content = urllib2.urlopen(req).read() # UTF-8
type = sys.getfilesystemencoding() # local encode format
print content.decode("UTF-8").encode(type) # convert encode format
import sys, urllib2
headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
req = urllib2.Request("http://blog.csdn.net/nevasun", headers=headers)
content = urllib2.urlopen(req).read() # UTF-8
type = sys.getfilesystemencoding() # local encode format
print content.decode("UTF-8").encode(type) # convert encode format
可以抓取中文页面了。
HTTP Error 403没有了,但是中文全都是乱码。又是怎么回事?的更多相关文章
- 爬虫遇到HTTP Error 403的问题
# coding=gbk from bs4 import BeautifulSoup import requests import urllib x = 1 y = 1 def crawl(url): ...
- asp.net mvc4 HTTP Error 403.14
asp.net mvc4项目部署到II&上时,出现HTTP Error 403.14 - Forbidden - The Web server is configured to not lis ...
- urllib.error.HTTPError: HTTP Error 403: Forbidden
问题: urllib.request.urlopen() 方法经常会被用来打开一个网页的源代码,然后会去分析这个页面源代码,但是对于有的网站使用这种方法时会抛出"HTTP Error 40 ...
- 解决github push错误The requested URL returned error: 403 Forbidden while accessing
来源:http://blog.csdn.net/happyteafriends/article/details/11554043 github push错误: git push error: The ...
- 解决git提交问题error: The requested URL returned error: 403 Forbidden while accessing
git提交代码时,出现这个错误"error: The requested URL returned error: 403 Forbidden while accessing https&qu ...
- PYCURL ERROR 22 - "The requested URL returned error: 403 Forbidden"
RHEL6.5创建本地Yum源后,发现不可用,报错如下: [root@namenode1 html]# yum install gcc Loaded plugins: product-id, refr ...
- remote: Permission to user_name/Code.git denied to other_user_name. fatal: unable to access 'https://github.com/user_name/Code.git/': The requested URL returned error: 403
Error msg: $ git push remote: Permission to xxx/Code.git denied to xxxxxx. fatal: unable to access ' ...
- XAMPP Access forbidden! Error 403,You don't have permission to access the requested directory
xampp 无论在window 还是在 Mac 如出现以下错误的:通常的解决方式: 具体配置教程可以任意查相关资料既可,(配置子站子大致流程如:开启httpd.conf的inc...httpd-vho ...
- python安装提示No module named setuptools,wget提示ERROR 403: SSL is required
在下载安装一个python工具时提示报错No module named setuptools [root@kermit supervisor-3.3.0]$ sudo python setup.py ...
随机推荐
- python 类型判断-- isinstance函数
判断类型 函数isinstance()可以判断一个变量的类型,既可以用在Python内置的数据类型如str.list.dict,也可以用在我们自定义的类,它们本质上都是数据类型. 假设有如下的 Per ...
- PHP入门 - - 06-->HTML的表格标签
表格标签<table> <table>的属性: Align: left, center, right (表格的)位置 Border: ...
- 使用recon/domains-hosts/baidu_site模块,枚举baidu网站的子域
使用recon/domains-hosts/baidu_site模块,枚举baidu网站的子域 [实例3-1]使用recon/domains-hosts/baidu_site模块,枚举baidu网站的 ...
- jQuery WipeTouch
有时,当你只想为触屏划动添加事件时,很多人可能会想到,Jquery mobile,但就这么个功能就把人家这么高大上的东西引用进来就有点大才小用了,WipeTouch是国外某程序员写的针对触屏划动的jq ...
- Android定位方式和测试方法
Android常用的三种定位方式有:基于GPS定位.基于基站地位.基于wifi定位. 1.基于GPS定位: GPS定位需要GPS模块(硬件)的支持,没有GPS模块是无法进行GPS定位的. GPS定位最 ...
- Python学习笔记07
时间: tickets 时间元组 格式化的时间 日历 import time tickets = time.time() print tickets print time.localtime() ...
- UVa 10870 & 矩阵快速幂
题意: 求一个递推式(不好怎么概括..)的函数的值. 即 f(n)=a1f(n-1)+a2f(n-2)+...+adf(n-d); SOL: 根据矩阵乘法的定义我们可以很容易地构造出矩阵,每次乘法即可 ...
- BZOJ 1086 & 类树的分块
题意: “余”人国的国王想重新编制他的 国家.他想把他的国家划分成若干个省,每个省都由他们王室联邦的一个成员来管理.他的国家有n个城市,编号为1..n.一些城市之间有道路相连,任意两个 不同的城市之间 ...
- [深入浅出WP8.1(Runtime)]应用实例——移动截图
10.2应用实例——移动截图 移动截图例子是实现一个把一张图片的某个部分截取出来的功能,并且用户可以选定截取的图片区间.那个该例子会使用ManipulationDelta事件来实现对截取区间的选择.然 ...
- 【BZOJ】1119: [POI2009]SLO
题意 长度为\(n(1 \le n \le 1000000)\)的账单,\(+\)表示存1,\(-\)表示取1,任意时刻存款不会为负.初始有\(p\),最终有\(q\).每一次可以耗时\(x\)将某位 ...