有的说是因为代理.
我的浏览器倒是经常开着代理, 但是我已经关闭了.
我特意查看了下HTTP报文, 也都是没经过代理的.
但还是会出错.
代码:
import urllib.request
url = "http://news.dbanotes.net/"
req = urllib.request.Request(url)
page = urllib.request.urlopen(req).read().decode("UTF-8")
print(page)
python版本: 3.5.1
报错信息:
urllib.error.HTTPError: HTTP Error 404: Not Found
应该可以排除以下的问题:
代理
网络问题
url不存在
代码有错
至于反爬虫这个我应该可能性也不大,
一来我试了很多个URL, 基本就是六四分, 有的能访问, 有的不能, 而且我们学校的官网那么, 我才不信他会...
二来我加了User-Agent的首部还是不能访问.
大家讲道理2017-04-18 09:42:11
There is no problem with my python 3.5.2 under windows.
It is recommended that you capture the packet and compare it with the request when accessed by the browser.
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
>>>
>>>
>>>
>>> import urllib.request
>>> url = "http://news.dbanotes.net/"
>>> req = urllib.request.Request(url)
>>> page = urllib.request.urlopen(req).read()
>>> page
b'<html><head><link rel="stylesheet" type="text/css" href="http://news.dbanotes.net/news.css">\n<script type="text/javascript" src="http://news.dbanotes.net/jailbreak.js"></script>\n<link rel="shortcut icon" ...'
伊谢尔伦2017-04-18 09:42:11
This may be related to the setting value of your agent, because some websites will check this to prevent non-browsers from crawling
巴扎黑2017-04-18 09:42:11
You copy the headers and cookies from the browser and add them to the Request object of urllib.
Simulated browser~~
天蓬老师2017-04-18 09:42:11
A very important reason is that the agent header you requested in your program has been blocked by the other party. Try changing the agent header.