和nodejs相比感觉怪怪的,最简单的get请求
python 3
import urllib.request
from urllib.error import HTTPError
import json
req = urllib.request.Request(url=r"http://api.ibeeger.com",headers={'User-Agent':' Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0'})
try:
handler = urllib.request.urlopen(req)
content = handler.read()
content = json.loads(content)
print(content['browser'])
except HTTPError as e:
content = e.read()
print(content)
nodejs
const http = require('http')
http.get({
hostname: 'api.ibeeger.com',
headers: {
'user-agent': 'Mozilla/5.0'
}
}, function(res) {
console.log(res.statusCode)
let body = '';
res.on('data', function(bf) {
body += bf
})
res.on('end', function() {
console.log(JSON.parse(body))
})
})
后面再对比其他方面吧