标签:
百度不支持用tornado请求,可以用美团开放API 测试。
1 import tornado.httpclient 2 3 def fetch(url): 4 http_header={‘User-Agent‘:‘Chrome‘} 5 http_request=tornado.httpclient.HTTPRequest(url=url,method=‘GET‘,headers=http_header,connect_timeout=200, request_timeout=600) 6 7 http_client=tornado.httpclient.HTTPClient() 8 9 http_response=http_client.fetch(http_request) 10 11 print http_response.code 12 13 all_fields=http_response.headers.get_all() 14 for field in all_fields: 15 print field 16 print http_response.body
import urllib2 def fetch(url): http_header = {‘User-Agent‘:‘Chrome‘} http_request = urllib2.Request(url,None,http_header) http_reponse = urllib2.urlopen(http_request) #Status code #200 OK #404 Invalid url #500 Internal error print(http_reponse.code) print(http_reponse.info()) print(http_reponse.read()) 调用: if __name__="__main__": fetch("http://www.meituan.com/api/v1/divisions")
标签:
原文地址:http://www.cnblogs.com/shined/p/4423755.html