2019-07-18 08:50:59 星期四
要系统学习urllib模块,我们从urlib基础开始。这个知识点中,我们会为大家实战讲解urlretrieve()、urlcleanup()、info()、getcode()当前网页状态码、geturl()当前网页网址等。
#网页截取 import urllib.request urllib.request.urlretrieve("http://www.hellobi.com",filename="F:/urllib2.html")(‘F:/urllib2.html’, <http.client.HTTPMessage at 0x2c9bc883080>)
200 正常网页 403 禁止访问*
#清缓存 urllib.request.urlcleanup() file=urllib.request.urlopen("http://www.hellobi.com") file.info()<http.client.HTTPMessage at 0x2c9bc883710>
由于网络速度或对方服务器的问题,我们爬取一个网页的时候,都需时间。我们访问一个网页,如果该网页长时间未响应,那么我们的系就会判断该网页超时了,即无法打开该网页。 有的时候,我们需要根据自己的需要,来设置超时的时间值,比如,有些网站反应快,我们希望2秒钟没有反应,则判断为超时,那么此时,timeout的值就是2,再比如,有些网站服务器反应慢,那么此时,我们希望100秒没有反应,才判断为超时,那么此时timeout的值就是100。接下来为大家实战讲解爬取时的超时设置。
file=urllib.request.urlopen("http://www.baidu.com",timeout=10) for i in range (0,100): try: file=urllib.request.urlopen("http://yum.iqianyue.com",timeout=1) data=file.read() print(len(data)) except Exception as e: print ("出现异常"+str(e))结果图示
客户端如果要与服务器端进行通信,需要通过http请求进行,http请求有很多种,我们在此会讲post与get两种请求方式。比如登陆、搜*索某些信息的时候会用到。
456008
#模拟get请求 中文搜索方法 import urllib.request keywd="南大鳥" keywd=urllib.request.quote(keywd)#中文转码 req=url="http://www.baidu.com/s?wd="+keywd+"&ie=utf-8"#把网址封装为一个请求 #urllib.request.Request #变成一个请求 data=urllib.request.urlopen(req).read() fh=open("F:/PL/PythonsearchChinese.html","wb") fh.write(data) fh.close() len(data)294958
2019-07-18 15:40:38 星期四
爬虫在运行的过程中,很多时候都会遇到这样或那样的异常。如果没有异常处理,爬虫遇到异常时就会直接崩溃停止运行,下次再次运行时,又会重头开始,所以,要开发一个具有顽强生命力的爬虫,必须要进行异常处理。
两者都是异常处理的类,HTTPError是URLError的子类,HTTPError有异常状态码与异常原因,URLError没有异常状态码,所以,在处理的时候,不能使用URLError直接代替HTTPError。如果要代替,必须要判断是否有状态码属性。
403 Forbidden
我们可以试试爬取csdn博客,我们发现会返回403,因为对方服务器会对爬虫进行屏蔽。此时,我们需要伪装成浏览器才能爬取。 浏览器伪装我们一般通过报头进行
#爬虫模拟浏览器 import urllib.request url="https://blog.csdn.net/" headers=("user-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36") opener=urllib.request.build_opener() opener.addheaders=[headers] data=opener.open(url).read() fh=open("F:/PL/browser1.html","wb") fh.write(data) fh.close() print("copy finished")由于urlopen()对于一些HTTP的高级功能不支持,所以,我们如果要修改报头,可以使用urllib.request.build_opener()进行,当然,也可以使用urllib.request.Request()下的add_header()实现浏览器的模拟。
#爬虫新浪新闻整个网页,把新闻下载到本地 import urllib.request import re data=urllib.request.urlopen("http://www.taptap.com/").read() data2=data.decode("utf-8","ignore") pat='herf="(http://www.taptap.com/.*?)">' allurl=re.compile(pat).findall(data2) print("every thing is ok") for i in range(0,len(allurl)): try: print("第"+str(i)+"次爬取") thisurl=allurl[i] file="F:/PL/sinanews/"+str(i)+".html" urllib.request.urlretrieve(thisurl,file) print("-----(❤ ω ❤)爬取成功-------") except urllib.error.URLError as e: if hasattr(e,"code"): print(e.code) if hasattr(e,"reason"): print(e.reason) print("news copy is ok")