<?php
class TestAction extends Action {
public function index(){
#$this->show('Hello world');
$name='怠忽待会';
echo $name;
#$this->assign('data',$name);
#$this->display();
}
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
import json
import httplib
import re
import requests
s=requests.session()
print s.headers
url = "http://127.0.0.1/DEVOPS/index.php/Test/index"
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
}
r = s.get(url,headers=headers)
r=r.content
print r
print type(r)
print r
print len(r)
返回的是utf8字符串,
C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a19.py
{'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.18.4'}
怠忽待会
<type 'str'>
怠忽待会
12
把utf8 字符串,转换为unicode
r = s.get(url,headers=headers)
r=r.content
print r
print type(r)
print r
print len(r)
r=r.decode('utf-8')
print r
print type(r)
print len(r)
C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a19.py
{'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.18.4'}
怠忽待会
<type 'str'>
怠忽待会
12
怠忽待会
<type 'unicode'>
4
在cmd运行:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
import json
import httplib
import re
import requests
s=requests.session()
print s.headers
url = "http://127.0.0.1/DEVOPS/index.php/Test/index"
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
}
r = s.get(url,headers=headers)
r=r.content
print r
print type(r)
print r
print len(r)
C:UsersTLCBDesktoppythonPython 高级编程4>python test.py
{'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*'
, 'User-Agent': 'python-requests/2.18.4'}
鎬犲拷寰呬細
<type 'str'>
鎬犲拷寰呬細
12
C:UsersTLCBDesktoppythonPython 高级编程4>
此时返回的是utf8,乱码,需要把utf8转为gbk
-------------------------------------------------------------------------------
r = s.get(url,headers=headers)
r=r.content
print r
print type(r)
print r
print len(r)
r=r.decode('utf-8').encode('gbk')
print r
print type(r)
print len(r)
C:UsersTLCBDesktoppythonPython 高级编程4>python test.py
{'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*'
, 'User-Agent': 'python-requests/2.18.4'}
鎬犲拷寰呬細
<type 'str'>
鎬犲拷寰呬細
12
怠忽待会
<type 'str'>
8
或者 把utf8转为unicode
r = s.get(url,headers=headers)
r=r.content
print r
print type(r)
print r
print len(r)
r=r.decode('utf-8')
print r
print type(r)
print len(r)
C:UsersTLCBDesktoppythonPython 高级编程4>python test.py
{'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*'
, 'User-Agent': 'python-requests/2.18.4'}
鎬犲拷寰呬細
<type 'str'>
鎬犲拷寰呬細
12
怠忽待会
<type 'unicode'>
4
C:UsersTLCBDesktoppythonPython 高级编程4>