网络爬虫urllib2 tornado

时间:2020-12-13 09:35:12

百度不支持用tornado请求,可以用美团开放API 测试。

 import tornado.httpclient

 def fetch(url):
http_header={'User-Agent':'Chrome'}
http_request=tornado.httpclient.HTTPRequest(url=url,method='GET',headers=http_header,connect_timeout=200, request_timeout=600) http_client=tornado.httpclient.HTTPClient() http_response=http_client.fetch(http_request) print http_response.code all_fields=http_response.headers.get_all()
for field in all_fields:
print field
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")