用python可以下载网页源码吗
python可以下载网页的源代码,使用urllib库,或者使用更为方便的requests库。
importurllib2 defdownload(url,num_retries=5): ''' function:下载网页源代码,如果遇到5xx错误状态,则继续尝试下载,直到下载num_retries次为止。 ''' print"downloading",url try: html=urllib2.urlopen(url).read() excepturllib2.URLErrorase: print"downloaderror:",e.reason html=None ifnum_retries>0: ifhasattr(e,'code')and500<=e.code<600: returndownload(url,num_retries-1) returnhtml
其中 url 即为你想现在的网页地址。 num_reties 为遇到 5xx 错误的时候,重试下载的次数。
更多学习内容,请点击python学习网。
本网站文章仅供交流学习 ,不作为商用, 版权归属原作者,部分文章推送时未能及时与原作者取得联系,若来源标注错误或侵犯到您的权益烦请告知,我们将立即删除.