网络爬虫urllib2 tornado

百度不支持用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")

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。