码迷,mamicode.com
首页 > Web开发 > 详细

网络爬虫urllib2 tornado

时间:2015-04-14 01:55:29      阅读:912      评论:0      收藏:0      [点我收藏+]

标签:

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

 

网络爬虫urllib2 tornado

标签:

原文地址:http://www.cnblogs.com/shined/p/4423755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!