python3爬虫 url管理器
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()))
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步