首先是简单的网页抓取程序:

[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没有了,但是中文全都是乱码。又是怎么回事?的更多相关文章

  1. 爬虫遇到HTTP Error 403的问题

    # coding=gbk from bs4 import BeautifulSoup import requests import urllib x = 1 y = 1 def crawl(url): ...

  2. asp.net mvc4 HTTP Error 403.14

    asp.net mvc4项目部署到II&上时,出现HTTP Error 403.14 - Forbidden - The Web server is configured to not lis ...

  3. urllib.error.HTTPError: HTTP Error 403: Forbidden

    问题:  urllib.request.urlopen() 方法经常会被用来打开一个网页的源代码,然后会去分析这个页面源代码,但是对于有的网站使用这种方法时会抛出"HTTP Error 40 ...

  4. 解决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  ...

  5. 解决git提交问题error: The requested URL returned error: 403 Forbidden while accessing

    git提交代码时,出现这个错误"error: The requested URL returned error: 403 Forbidden while accessing https&qu ...

  6. PYCURL ERROR 22 - "The requested URL returned error: 403 Forbidden"

    RHEL6.5创建本地Yum源后,发现不可用,报错如下: [root@namenode1 html]# yum install gcc Loaded plugins: product-id, refr ...

  7. 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 ' ...

  8. XAMPP Access forbidden! Error 403,You don't have permission to access the requested directory

    xampp 无论在window 还是在 Mac 如出现以下错误的:通常的解决方式: 具体配置教程可以任意查相关资料既可,(配置子站子大致流程如:开启httpd.conf的inc...httpd-vho ...

  9. 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 ...

随机推荐

  1. java 汽车销售收入系统

     >>>>>>>>>>>>>>>>>>>> 语言:java 工具:eclipse ...

  2. nefu 197 KMP

    Description 在信息检索系统中一个很重要的环节就是关键字符串的查找,从而很多对自己有用的信息.给你一个很长的一段文字, 和一个关键信息字符串,现在要你判断这段文字里面是否有关键字符串. In ...

  3. POJ 1159 回文串-LCS

    题目链接:http://poj.org/problem?id=1159 题意:给定一个长度为N的字符串.问你最少要添加多少个字符才能使它变成回文串. 思路:最少要添加的字符个数=原串长度-原串最长回文 ...

  4. Swift3.0语言教程使用占位符格式创建和初始化字符串

    Swift3.0语言教程使用占位符格式创建和初始化字符串 Swift3.0语言教程使用占位符格式创建和初始化字符串在很多的编程语言中都存在占位符,占位符就是为指定的内容占留一个位置.此功能一般在开发者 ...

  5. 后缀数组 POJ 1743 Musical Theme

    题目链接 题意:给定n个数字,求超过5个数字的,最长的,变化相同的,不相交的重复子串 分析:男人8题中的一题!数列相邻两项做差,形成新数列,即求数列中的最长重复子串(不可相交). 后缀数组+二分答案. ...

  6. Trie + DP LA 3942 Remember the Word

    题目传送门 题意:(训练指南P209) 问长字符串S能由短单词组成的方案数有多少个 分析:书上的做法.递推法,从后往前,保存后缀S[i, len-1]的方案数,那么dp[i] = sum (dp[i+ ...

  7. 如何在UMG中上显示3D物体

    http://www.unrealchina.net/forum.php?mod=viewthread&tid=101548 个人认为可以使用关卡系统,搞个空关卡来做渲染比较好

  8. gridview自定义排序

    效果如图: 首先允许排序:AllowSorting="True":开启gridview的排序事件onsorting="GridView1_Sorting",也可 ...

  9. 【SQL】区分新来顾客和再访顾客

    -- 赋值 客户来访记录 SELECT m.* FROM (SELECT x.*, CASE WHEN x.ts > (SELECT MIN(a.ts) FROM USER.ps_afterre ...

  10. 网易大手笔领投美国VR触觉公司AxonVR

    12月8日消息,美国西雅图VR触觉公司AxonVR今日宣布完成了580万美元的种子轮融资,由中国网易和Dawn Patrol Ventures领投.此次融资完成后AxonVR的总融资额将攀升至700万 ...