#python中Urllib库实战#系统学习urllib模块,从urllib基础开始。学习urlretrieve(),urlcleanup(),info(),getcode(),geturl()import urllib.request#urlretrieve() 直接将一个网页爬到本地urllib.request.urlretrieve("http://www.hellobi.com",filename="/Users/xubin/myapp/pythonfile/urlretrieve.html")#urlcleanup() 将urlretrieve产生的缓存,清空urllib.request.urlcleanup()#info() 将一些基础的环境信息展示粗来file=urllib.request.urlopen("http://www.hellobi.com")print(file.info())#getcode() 获取访问url的状态码,返货200,print(file.getcode())#geturl() 获取爬取得网址print(file.geturl())#超时设置#爬取一个网页,需要时间。访问网页,网页长时间未响应,系统判断网页超时了,无法打开网页。#服务器反应快设置2秒没反应未超时,如果服务器反应慢设置100秒没反应未超时,timeout超时时间为2 100file=urllib.request.urlopen("http://www.hellobi.com",timeout=1)for i in range(0,10): try: file=urllib.request.urlopen("http://yum.iqianyue.com",timeout=0.1) data=file.read() print(len(data)) except Exception as e: print("出现异常:"+str(e))#自动模拟http请求#客户端如果要与服务器端进行通信,需要通过http请求进行,http请求有很多种#主要涉及post,get两种方式,比如登录,搜索某些信息的时候会用到#一般登录某个网站的时候,需要post请求#一般搜索某些信息的时候,需要get请求#在百度上搜索关键词,用python实现,需要用到请求,get get请求URL中有?#https://www.baidu.com/s?wd=pythonimport urllib.requestimport rekeywd="徐彬"keywd=urllib.request.quote(keywd)url="http://www.baidu.com/s?wd="+keywd #注意不能用httpsreq=urllib.request.Request(url)data=urllib.request.urlopen(req).read()fh=open("/Users/xubin/myapp/pythonfile/百度python.html","wb")fh.write(data)fh.close()#post请求 比如需要登录用户 需要提交post请求#http://passport.csdn.net/account/login 用户名:username 密码:passwordimport urllib.requestimport urllib.parseurl="https://passport.csdn.net/account/login"mydata=urllib.parse.urlencode({"username":"bingoxubin","password":"19900127LLBingo"}).encode("utf-8")req=urllib.request.Request(url,mydata)data=urllib.request.urlopen(req).read()fh=open("/Users/xubin/myapp/pythonfile/csdn登录界面.html","wb")fh.write(data)fh.close()'''#爬取oa上的所有照片,存到OA照片.docx中 #遇到问题,目前所学,只能爬取单页的内容import reimport urllib.requestdata=urllib.request.urlopen("oa.epoint.com.cn").read()data=data.decode("utf-8")pat=""mydata=re.compile(pat).findall(data)fh=open("/Users/xubin/myapp/pythonfile/OA照片.docx","w")for i in range(0,len(mydata)): fh.write(mydata[i]+"
")fh.close()''' 继续阅读与本文标签相同的文章
上一篇 :
生成式AI真的是机器智能的关键所在吗?
下一篇 :
Java 内联类初探
-
新机保贝获近亿元A轮融资,经纬领投
2026-05-25栏目: 教程
-
零基础Python教程034期 random随机数开发猜数字游戏
2026-05-25栏目: 教程
-
法国速度与中国创新——揭秘东风雪铁龙与阿里、斑马首款合资互联网汽车
2026-05-25栏目: 教程
-
一次讲透次短路及条数问题,详细探讨dijkstra算法的本质
2026-05-25栏目: 教程
-
微服务架构案例(04):中间件集成,公共服务封装
2026-05-25栏目: 教程
