[Python] python urllib2 伪造header →→→→→进入此内容的聊天室

来自 , 2021-03-28, 写在 Python, 查看 139 次.
URL http://www.code666.cn/view/022898bb
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #encoding=utf-8
  4. #Filename:urllib2-header.py
  5.  
  6. import urllib2
  7. import sys
  8.  
  9. #抓取网页内容-发送报头-1
  10. url= "http://www.phpno.com"
  11. send_headers = {
  12.     'Host':'www.phpno.com',
  13.     'User-Agent':'Mozilla/5.0 (Windows NT 6.2; rv:16.0) Gecko/20100101 Firefox/16.0',
  14.     'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  15.     'Connection':'keep-alive'
  16. }
  17.  
  18. req = urllib2.Request(url,headers=send_headers)
  19. r  = urllib2.urlopen(req)
  20.  
  21. html = r.read()                             #返回网页内容
  22. receive_header = r.info()                   #返回的报头信息
  23.  
  24. # sys.getfilesystemencoding()
  25. html = html.decode('utf-8','replace').encode(sys.getfilesystemencoding())   #转码:避免输出出现乱码
  26.  
  27. print receive_header
  28. # print '####################################'
  29. print html
  30. #//python/5716

回复 "python urllib2 伪造header"

这儿你可以回复上面这条便签

captcha