zoukankan      html  css  js  c++  java
  • 网络爬虫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")
  • 相关阅读:
    Redis持久化
    Redis配置文件详解
    Linux
    有图有真相
    Redis五大数据类型
    Redis基本知识
    Mysql主从复制
    Python脚本实现KVM虚机添加磁盘
    JQuery制作环形进度条
    JQuery制作标签
  • 原文地址:https://www.cnblogs.com/shined/p/4423755.html
Copyright © 2011-2022 走看看