在python中测试用户代理欺骗

时间:2015-06-17 08:52:39

标签: python user-agent

我是python和使用python 3的新手。我试图下载一个网页,我想知道的是,如果有办法真正看到用户代理的名称,那么系统管理员或谷歌看到它。在我的代码中,我下载并将网页保存为如下文本文件:

#Import 
from urllib.request import urlopen,Request

url1 = urlopen(Request(url,  headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}))

 #Create file and write
 f=open('mah_a.txt','wb')
 f.write(url1.read())
 f.close()

如何查看我的用户代理名称是否已更改?

1 个答案:

答案 0 :(得分:0)

您更改了User-Agent标头,是的。如果要查看服务器收到的内容,可以使用httpbin.org之类的在线回显服务器:

url = 'http://httpbin.org/get'
url1 = urlopen(Request(url,  headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}))
print(url1.read().decode('utf8'))

演示:

>>> from urllib.request import urlopen,Request
>>> url = 'http://httpbin.org/get'
>>> url1 = urlopen(Request(url,  headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}))
>>> print(url1.read().decode('utf8'))
{
  "args": {}, 
  "headers": {
    "Accept-Encoding": "identity", 
    "Host": "httpbin.org", 
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36"
  }, 
  "origin": "188.29.165.166", 
  "url": "http://httpbin.org/get"
}