标签:
import urllib.request #python3中将urllib2拆分为了urllib.request、urllib.error、urllib.response等 import http.cookiejar url = "http://www.baidu.com" print("第一种方法") response1 = urllib.request.urlopen(url) print(response1.getcode()) #打印response1的状态码看是否请求成功, 200表示请求成功 print(len(response1.read())) #打印返回网页内容长度 print("第二种方法") request = urllib.request.Request(url) request.add_header("user-agent", "Mozilla/5.0") #模拟浏览器访问 request2 = urllib.request.urlopen(request) print(request2.getcode()) print(len(request2.read())) print("第三种方法") cj = http.cookiejar.CookieJar() opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) #在请求对象中添加cookie urllib.request.install_opener(opener) request3 = urllib.request.urlopen(url) print(request3.getcode()) print(len(request3.read()))
标签:
原文地址:http://www.cnblogs.com/lsy-ai/p/5666163.html