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 ...
随机推荐
- js Array对象
http://www.w3cschool.cc/js/js-obj-array.html 创建新方法 原型是JavaScript全局构造函数.它可以构建新Javascript对象的属性和方法. 实例: ...
- SpringJMS解析3-监听器
消息监听器容器是一个用于查看JMS目标等待消息到达的特殊bean,一旦消息到达它就可以获取到消息,并通过调用onMessage()方法将消息传递给一个MessageListener实现.Spring中 ...
- node.js整理 01代码的组织和部署
模块 require(函数) 用于在当前模块中加载和使用别的模块,传入一个模块名,返回一个模块导出对象. 模块名可使用相对路径(以./开头),或者是绝对路径(以/或C:之类的盘符开头:注意单个模块名默 ...
- 对于div的右浮动会导致顺序会改变
当我们设置几个div右浮动的时候会出现顺序的改变,直接倒序了. 解决的方法是在几个div外面加上一个大的div即可,但是里面的所有div都要左浮动才行,具体做法如下: <!DOCTYPE htm ...
- 当 NSDictionary 遇见 nil
Demo project: NSDictionary-NilSafe 问题 相信用 Objective-C 开发 iOS 应用的人对下面的 crash 不会陌生: *** -[__NSPlacehol ...
- 疯狂java学习笔记之面向对象(四) - this关键字
Java中this关键字主要有以下两个方法: 1.this引用 - 可用于任何非static修饰的方法和构造器中,当this用于方法中时,它代表调用该方法的实例/对象;当this用于构造器中时,它代表 ...
- BZOJ3567 : AABB
考虑以块大小为$32$将序列分块,设$s[i][j]$表示前$i$块和前$j$块矩形相交的对数,$f[i][j]$表示矩形$i$和前$j$块的相交个数. 如果矩形$i$和$j$相交,那么有: $x_1 ...
- 163源报错Hash Sum mismatch 解决方法
Ubuntu server 用的163的源,报错: W: Failed to fetch http://mirrors.163.com/ubuntu/dists/precise-updates/mai ...
- HTTP请求头详解
http://blog.csdn.net/kfanning/article/details/6062118 HTTP由两部分组成:请求和响应.当你在Web浏览器中输入一个URL时,浏览 器将根据你的要 ...
- 【noiOJ】p1776
t1776:木材加工 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 木材厂有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目是给 ...