What is the trick in this website?
I can access this webpage in my firefox browser: http://www.ip-adress.com/ip_tracer/74.82.190.99 So I can get the information about this IP.
However, when I fetch it using Pyth开发者_运维问答on, there will be errors:
import urllib
f = urllib.urlopen("http://www.ip-adress.com/ip_tracer/74.82.190.99")
print f.read()
I get this error:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /ip_tracer/74.82.190.99
on this server.</p>
</body></html>
I take a look into the page's source code:
<form action="/ip_tracer/" method="post">
<div>
<input id="ipqry" name="QRY" type="text" value="74.82.190.99" size="18" maxlength="255" onclick="cleanup(this)">
<input type="submit" value="Track IP, host or website" onclick="progress(true)">
</div>
</form>
And I use the POST
method, the result is the same:
import urllib
params = urllib.urlencode({'QRY': '74.82.190.99'})
f = urllib.urlopen("http://www.ip-adress.com/ip_tracer/", params)
print f.read()
The result is the same 403 Forbidden
.
Can anyone give me a hint? I am using Python 2.5 on Windows XP.
Thanks a lot!
Probably the server reads your User-Agent
header and decides not to serve your request. Alternatively it can rely on some other headers being typically set by normal browsers (like FF).
I tried this one:
import urllib2
request = urllib2.Request("http://www.ip-adress.com/ip_tracer/74.82.190.99")
request.add_header("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5")
f = urllib2.urlopen(request)
print f.read()
and got the proper result.
NOTE: Please check the Terms of Service of the site if you plan to use it programmatically. It might violate their rules if you keep sending such requests automatically.
It probably read the origin of the POST
command and sees that it doesn't come from a valid host and denies you.
精彩评论